2017-08-07 49 views
0

我试图使用代表多米诺骨牌的自定义视图类,遵循this答案的解决方案。没有错误,应用程序运行但没有显示。由于它与链接中引用的问题非常类似,我想这只是一个小问题,但我在代码中找不到错误。CustomView不起作用

的Domino.java:

package com.example.android.dominoneu; 

import android.content.Context; 
import android.widget.LinearLayout; 


public class Domino extends LinearLayout { 

    public Domino(Context context){ 

     super(context); 
    } 
} 

的domino.xml:

<?xml version="1.0" encoding="utf-8"?> 
<com.example.android.dominoneu.Domino xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:Domino="http://schemas.android.com/apk/res-auto" 
    android:orientation="horizontal" android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="LEFT" 
     android:textColor="#000000"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RIGHT" 
     android:textColor="#000000"/> 

</com.example.android.dominoneu.Domino> 

在MainActivity,它补充说:

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 

public class MainActivity extends AppCompatActivity { 

    private RelativeLayout board; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     board = (RelativeLayout) findViewById(R.id.board); 
     board.addView(new Domino(this)); 

    } 
} 
+0

domino.xml不activity_main.xml中也许这是你的问题 – LLL

回答

1

我可能是错的,但你只是扩展LinearLayout(就是你想要的?)。 如果您希望每次引用Domino视图时显示自定义布局,则需要在构造函数中使布局膨胀。从其他的问题,请参见下面的代码:

public Card(Context context) { 
    super(context); 

    View view = LayoutInflater.from(getContext()).inflate(
      R.layout.card, null); 

    this.addView(view); 

} 

这是没有必要需要使用“this.addView(图)。”像另一个问题一样,如果你使用下面的膨胀方法。

LayoutInflater.from(getContext()).inflate(R.layout.card, this, true); 

所以,你domino.xml会有如下(举例):

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <TextView android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="LEFT" 
     android:textColor="#000000"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RIGHT" 
     android:textColor="#000000"/> 
</LinearLayout> 

的Domino类(例如):

public class Domino extends LinearLayout { 

    public Domino (Context context) { 
     super(context); 

     View view = LayoutInflater.from(getContext()).inflate(R.layout.domino, this, true); 

     // Customize your view, E.g.: 
     TextView textView = view.findViewById(R.id.text); 
     textView.setText("Foobar domino"); 
    } 

    // add the following if you like to reference Domino View in a Layout 
    public Domino(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 

     View view = LayoutInflater.from(getContext()).inflate(R.layout.domino, this, true); 

     // Customize your view, E.g.: 
     TextView textView = view.findViewById(R.id.text); 
     textView.setText("Foobar domino"); 
    } 
} 

而且你的活动将是相同的(如果您试图动态添加Domino视图)或在您的MainActivity布局中引用Domino视图。

好的编码。

+0

你说得对的信息,这是我想要什么....我混淆了这个问题(动态地添加视图和使用它作为XML) – Reinmarius

1

添加Domino(Context context, AttributeSet attrs)构造你的看法。这被XML用来创建视图的一个实例。

而且也没有必要与addView手动添加它,如果你使用XML

1

您应该创建这个构造

public Domino(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
super(context, attrs, defStyleAttr); 
/*do here all what you want with custom view*/ 
} 
+0

只有1个构造与语境和的AttributeSet足够 –

1

你要添加的所有4层超级构造是这样的。

public class Domino extends LinearLayout { 
    public Domino(Context context) { 
     super(context); 
    } 

    public Domino(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public Domino(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
    public Domino(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 
} 
+0

只有1个构造与语境和的AttributeSet足够 –

+0

感谢@PotapovAnton –