【发布时间】:2011-08-09 18:37:45
【问题描述】:
我有一个活动,底部有一个保存和取消按钮。
在 AlertDialog 中,按钮显示在某种样式的容器视图中。
如何使我的 Activity 中的按钮具有相同的外观?具体来说,如何在 AlertDialog 中应用按钮容器视图的样式来在包含按钮的 Activity 中说出 LinearLayout?
谢谢
【问题讨论】:
我有一个活动,底部有一个保存和取消按钮。
在 AlertDialog 中,按钮显示在某种样式的容器视图中。
如何使我的 Activity 中的按钮具有相同的外观?具体来说,如何在 AlertDialog 中应用按钮容器视图的样式来在包含按钮的 Activity 中说出 LinearLayout?
谢谢
【问题讨论】:
elsewhere 提供了可行的解决方案。简而言之,您可以简单地在 xml 中使用 style 属性来实现这一点。例如,style="?android:attr/buttonBarStyle" 和 style="?android:attr/buttonBarButtonStyle" 将完成这项工作(对于 API 11+)。这是两个按钮水平放在一起的示例。
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:paddingTop="0dip" >
<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text= "Ok" />
<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
唯一剩下的就是alertDialog 中的按钮正上方有一条水平线,上面的代码不会创建它。如果你想拥有那条水平线,应该在 xml 中手动添加,在 LinearLayout 上方。这将为您提供水平线:
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="0dp"
android:background="?android:attr/dividerVertical" />
【讨论】:
我会做这样的事情:
LinearLayout dialogLayout = (LinearLayout) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_addeditrecord, null);
然后我使用 dialogLayout 调用 findViewById() 来拉入按钮和其他视图并设置 OnClickListener 等...
然后显示对话框:
builder = new AlertDialog.Builder(this);
builder.setView(dialogLayout);
builder.create().show();
【讨论】: