gpt4 book ai didi

android - 程序化 Android 布局

转载 作者:行者123 更新时间:2023-11-30 04:32:59 26 4
gpt4 key购买 nike

我试图将 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com