2017-08-07 53 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 –