2013-03-13 34 views
0

我試圖通過延長tablerow的視圖編程創建這個XML視圖:延長tablerow的觀點

<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通過addView功能。

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不會添加到活動sceen。

回答

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); 
    } 
} 
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);

看看是否能工程。

+0

nope,沒有幫助 – vlio20 2013-03-13 10:51:18