android - 程序化 Android 布局

标签 android layout user-interface android-linearlayout tablerow

我试图将 Android XML 的外观与以编程方式创建它的方式关联起来,但我惨遭失败。我读过几个引用资料,指出 View 的 LayoutParams 类型必须等于父级的类型,但我只是不理解它。

给定以下 XML,我将如何以编程方式重新创建它?

<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>

这是我尝试过的:

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

没有错误,但与 XML 不匹配。相反,它看起来像这样: enter image description here

第一个来自 XML,第二个是我尝试以编程方式复制它。

最佳答案

LayoutInflater 是最简单的方法:

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

关于android - 程序化 Android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7369635/

相关文章:

css - 如何让多个样式表在单个页面上工作?

java - RecyclerView 之上的透明 TextView

java - Android 绘制 2D 图形

java - 用户在 JFrame 中未输入任何内容并收到错误消息

java - 单击列表项显示更多信息

java.lang.RuntimeException : Unable to start activity

android - achartengine : the label of axis dont appear if the graphic is empty 图形问题

android - 即使我的应用程序关闭,广播接收器也可以工作

java - 在 Android 应用程序中使用处理程序的 ProgressBar 倒计时

layout - CSS 内联对齐