开发者

Programmatic Android Layout

I'm trying to correlate how the Android XML looks versus creating it programmatically and I'm failing miserably. I've rea开发者_StackOverflow中文版d several references stating that the type for the LayoutParams for the View must be equal to those of the parent, but I'm just not grokking it.

Given the following XML, how would I re-create this programmatically?

<TableRow
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_gravity="center_vertical"
>
    <LinearLayout  
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center_vertical"
        android:paddingTop="3dp"
        android:orientation="vertical"
    >
        <EditText 
            android:padding="2dp"
            android:layout_width="100dp"
            android:layout_height="35dp"
            android:layout_column="0"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:textSize="15dp"
            android:numeric="integer"
        />
    </LinearLayout>

    <LinearLayout 
        android:layout_height="35dp"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:padding="2dp"
        android:orientation="vertical"
        >

        <CheckBox android:text="Check1" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_weight="1"
            android:textSize="10sp" 
            android:background="@drawable/checkbox_background" 
            android:button="@drawable/checkbox" 
            android:textColor="@color/Gray"
        />
        <CheckBox android:text="Check2" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="10sp" 
            android:background="@drawable/checkbox_background" 
            android:button="@drawable/checkbox" 
            android:textColor="@color/Gray"
        />
    </LinearLayout>

Here's what I've tried:

// Get the TableLayout
TableLayout tl = (TableLayout)findViewById(R.id.score_table);

LayoutParams lp;
TableRow.LayoutParams tp;

// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100);
tr.setLayoutParams(new TableRow.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

LinearLayout ll0_1 = new LinearLayout(this);
// If the layout params of the child should be of
// the type of the parent this should be 
// TableRow.LayoutParams?
tp = (TableRow.LayoutParams)tr.getLayoutParams();
tp.height = TableRow.LayoutParams.WRAP_CONTENT;
tp.width = TableRow.LayoutParams.FILL_PARENT;
tp.gravity = Gravity.CENTER_VERTICAL;
// TableRow.LayoutParams has no padding so this
// gets set on the LinearLayout?
ll0_1.setPadding(0,3,0,0);
ll0_1.setLayoutParams(tp);
tr.addView(ll0_1);

EditText et0 = new EditText(this);
et0.setId(100);
et0.setInputType(InputType.TYPE_CLASS_NUMBER);
// Same as above; LP set to type of parent
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT, 1f);
lp.height = 35;
lp.width = 100;
// Same as above; the parent's type doesn't have 
// these attributes
et0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
et0.setPadding(0, 3, 0, 0);
et0.setLayoutParams(lp);
ll0_1.addView(et0);

LinearLayout ll0_2 = new LinearLayout(this);
ll0_2.setOrientation(LinearLayout.VERTICAL);
tr.addView(ll0_2);

CheckBox cb0_1 = new CheckBox(this);
cb0_1.setTextSize(10f);
cb0_1.setTextColor(Color.GRAY);
cb0_1.setBackgroundResource(R.drawable.checkbox_background);
cb0_1.setButtonDrawable(R.drawable.checkbox);
cb0_1.setText("Nil");
ll0_2.addView(cb0_1);

CheckBox cb0_2 = new CheckBox(this);
cb0_2.setTextSize(10f);
cb0_2.setTextColor(Color.GRAY);
cb0_2.setBackgroundResource(R.drawable.checkbox_background);
cb0_2.setButtonDrawable(R.drawable.checkbox);
cb0_2.setText("10fer");
ll0_2.addView(cb0_2);

// Duplicate code removed for brevity
//...

// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

There is no error, but it does not match the XML. Instead, it looks like this:

Programmatic Android Layout

The first one is from the XML, the second one is my attempt at reproducing it programmatically.


LayoutInflater is the easiest way :

public View myView(){
       View v;
       LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       v = inflater.inflate(R.layout.myview, null);
       TextView text1 = (TextView) v.findViewById(R.id.dolphinTitle);
       Button btn1 = (Button) v.findViewById(R.id.dolphinMinusButton);
       TextView text2 = (TextView) v.findViewById(R.id. dolphinValue);
       Button btn2 = (Button) v.findViewById(R.id. dolphinPlusButton);
return v;
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜