Android在运行时将按钮插入线性布局
我在xml文件中定义了一个线性布局,如下所示Android在运行时将按钮插入线性布局,android,Android,我在xml文件中定义了一个线性布局,如下所示 <LinearLayout android:id="@+id/manageStoresAlphabeticPagination" android:layout_width="fill_parent" android:orientation="horizontal" android:weightSum="26" android:layout_height="wr
<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>
现在,Linear布局的内容将包含所有字母表。我不想在我的布局中定义它们,而是通过我的代码。如何通过我的代码创建按钮,应用样式,然后将其添加到线性布局
亲切的问候
----------------编辑--------------------------------
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);
}
LinearLayout storePaginationLayout=(LinearLayout)视图
.findViewById(R.id.manageStoresAlphabeticPagination);
对于(int i=0;i<22;i++){
Button b1=新建按钮(this.getActivity(),null,
R.风格。字母页码风格);
b1.setLayoutParams(新的LinearLayout.LayoutParams)(LayoutParams.WRAP_内容,
LayoutParams.WRAP_内容,1f));
b1.setText(“A”);
storePaginationLayout.addView(b1);
}
未从代码中应用样式。但是,从xml布局来看,它可以正常工作吗
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);