Activity的AlertDialog样式按钮

时间:2011-04-20 19:12:59

标签: java android

我的活动底部有一个“保存并取消”按钮。

在AlertDialog中,按钮显示在某种样式的容器视图中。

我怎么能在我的Activity中给出相同外观的按钮?具体来说,我如何在AlertDialog中应用按钮容器视图的样式来说明包含按钮的Activity中的LinearLayout?

由于

2 个答案:

答案 0 :(得分:31)

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中按钮正上方有一条水平线,上面的代码不会创建。如果您想拥有该水平线,则应在LinearLayout上方的xml中手动添加。这将为您提供水平线:

<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_marginBottom="0dp"
    android:background="?android:attr/dividerVertical" /> 

答案 1 :(得分:1)

我做了这样的事情:

LinearLayout dialogLayout = (LinearLayout) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_addeditrecord, null);

然后我使用dialogLayout调用findViewById()来拉入按钮和其他视图并设置OnClickListeners等......

然后显示对话框:

builder = new AlertDialog.Builder(this);

builder.setView(dialogLayout);

builder.create().show();