0

テーブル行ビューを拡張して、この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);
    }
}

私のアクティビティでは、 by関数にCategoryRowビューを追加しています。TableLayoutaddView

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 に答える