我在xml文件中定义了一个线性布局,如下所示
<LinearLayout
android:id="@+id/manageStoresAlphabeticPagination"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="26"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/searchBarFrameManageStores"
android:layout_toRightOf="@+id/searchBarFrameManageStores" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:layout_weight="1"
style="@style/alphaPaginationStyle" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:layout_weight="1"
style="@style/alphaPaginationStyle" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:layout_weight="1"
style="@style/alphaPaginationStyle" />
</LinearLayout>
现在,lineaar布局的内容将包含所有字母表。我不想在我的布局中定义它们而不是通过我的代码。如何通过我的代码创建按钮,应用样式,然后将其添加到线性布局?
亲切的问候
---------------- EDIT ------------------------------ -
LinearLayout storePaginationLayout = (LinearLayout) view
.findViewById(R.id.manageStoresAlphabeticPagination);
for (int i = 0; i < 22; i++) {
Button b1 = new Button(this.getActivity(), null,
R.style.alphaPaginationStyle);
b1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,1f));
b1.setText("A");
storePaginationLayout.addView(b1);
}
未从代码中应用样式。但是从xml布局它可以正常工作吗?
答案 0 :(得分:1)
Button button = new Button(this, null, R.style.alphaPaginationStyle);
LinearLayout.LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setText(yourAlphabet);
yourLinearLayout.addView(button);