私はAndroidでカスタム表示をしています。これは、LinearLayout内の2つのTextViewの単純なコンボです。カスタム表示で何も表示されません - Android
__________________________________
|TextView |TextView |
----------------------------------
マイカスタムクラスは以下の通りです:
public class Label extends View {
private TextView label, display;
LinearLayout layout;
public Label(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.Label,
0,0
);
try{
layout = new LinearLayout(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
layout.setPadding(4,4,4,4);
layout.setLayoutParams(params);
layout.setOrientation(LinearLayout.VERTICAL);
label = new TextView(context);
label.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
display = new TextView(context);
display.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
label.setText(a.getString(R.styleable.Label_label_text));
display.setText(a.getString(R.styleable.Label_display_text));
layout.addView(label);
layout.addView(display);
}finally{
a.recycle();
}
}
public void setDisplay(String displayText){
display.setText(displayText);
invalidate();
requestLayout();
}
public void setLabel(String labelText){
label.setText(labelText);
invalidate();
requestLayout();
}
}
これは私のattrのセットです:
<resources>
<declare-styleable name="Label">
<attr name="label_text" format="string"/>
<attr name="display_text" format="string"/>
<attr name="drawable" format="color"/>
<attr name="label_position" format="enum">
<enum name="left" value="-1"/>
<enum name="center" value ="0"/>
<enum name="right" value="1"/>
</attr>
<attr name="display_position" format="enum">
<enum name="left" value="-1"/>
<enum name="center" value ="0"/>
<enum name="right" value="1"/>
</attr>
</declare-styleable>
</resources>
そして、これは私がそれを追加している方法です。
を<com.wally.pocket.widgets.Label
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:label_text="Test"
app:display_text="Test" />
しかしプレビュー画面では何も表示されません。それは描画ではありません。私はアプリを起動すると何も表示されません。私は何が欠けていますか?
私は高さまたは幅が0だと思います。あなたは 'onMeasure()'を実装する必要があります –