我正在尝试通过扩展 tablerow 视图以编程方式创建此 xml 视图:
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:weightSum="1.0" >
<Button
android:id="@+id/tester_button"
style="?android:attr/buttonStyleSmall"
android:layout_weight=".2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="A" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:gravity="center"
android:layout_weight=".7"
android:text="Column 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_weight=".1"
android:layout_width="0dip"
android:layout_height="wrap_content" />
</TableRow>
这是我的扩展行类:
public class CategoryRow extends TableRow {
LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
LayoutParams innerParams = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
CheckBox check;
Button arrowButton;
TextView categoryTitle;
public CategoryRow(Context context) {
super(context);
this.setLayoutParams(rowParams);
this.setGravity(Gravity.CENTER);
this.setWeightSum(1);
check = new CheckBox(context);
innerParams.weight = (float) 0.1;
check.setLayoutParams(innerParams);
arrowButton = new Button(context);
innerParams.weight = (float) 0.2;
arrowButton.setLayoutParams(innerParams);
arrowButton.setText("A");
categoryTitle = new TextView(context);
innerParams.weight = (float) 0.7;
categoryTitle.setLayoutParams(innerParams);
categoryTitle.setTextAppearance(context, android.R.attr.textAppearanceLarge);
//add views to row
this.addView(arrowButton);
this.addView(categoryTitle);
this.addView(check);
}
public void setCategoryTitle(String title){
categoryTitle.setText(title);
}
public void checkCategory(){
check.setChecked(true);
}
public void unCheckCategory(){
check.setChecked(false);
}
}
在我的活动中,我将CategoryRow
视图添加到TableLayout
byaddView
函数。
public class NotificationCategorise extends Activity {
TableLayout categoryConteiner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_categorise);
categoryConteiner = (TableLayout) findViewById(R.id.categories_conteiner);
CategoryRow categoryRow = new CategoryRow(this);
categoryRow.setCategoryTitle("bla");
categoryConteiner.addView(categoryRow);
}
我的问题是它CategoryRow
没有添加到活动场景中。