0

我正在尝试通过扩展 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视图添加到TableLayoutbyaddView函数。

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没有添加到活动场景中。

4

2 回答 2

1

我已将扩展类更改为此代码,并且效果很好。

import android.content.Context;
import android.view.Gravity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TableRow;
import android.widget.TextView;

public class CategoryRow extends TableRow {
    LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    LayoutParams innerParams1 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
    LayoutParams innerParams2 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
    LayoutParams innerParams3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, Gravity.CENTER);
    CheckBox check;
    Button arrowButton;
    TextView categoryTitle;

    public CategoryRow(Context context) {
        super(context);
        this.setLayoutParams(rowParams);
        this.setWeightSum(1f);

        check = new CheckBox(context);
        innerParams1.weight = 0.1f;
        check.setLayoutParams(innerParams1);

        arrowButton = new Button(context);
        innerParams2.weight = 0.2f;
        arrowButton.setLayoutParams(innerParams2);
        arrowButton.setText("A");

        categoryTitle = new TextView(context);
        categoryTitle.setGravity(Gravity.CENTER);
        innerParams3.weight = 0.7f;
        categoryTitle.setLayoutParams(innerParams3);
        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);
    }
}
于 2013-03-13T21:00:20.040 回答
0

好的,我认为您不能将作为该行的构造函数GRAVITY.CENTER的参数传入。LayoutParams您可以为添加到该行的每个单独的视图更改它,但不会为该行本身更改它。改变:

LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);

LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

看看这是否有效。

于 2013-03-13T10:25:50.447 回答