我有一个活动,底部有一个Save and Cancel按钮.
在AlertDialog中,按钮显示在某种样式的容器视图中.
我怎么能在我的Activity中给出相同外观的按钮?具体来说,我如何在AlertDialog中应用按钮容器视图的样式来说明包含按钮的Activity中的LinearLayout?
谢谢
had*_*adi 31
其他地方有解决方案可行.简而言之,您可以简单地使用stylexml中的属性来实现此目的.例如,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>
Run Code Online (Sandbox Code Playgroud)
唯一剩下的就是,按钮上方有一条水平线alertDialog,上面的代码不会创建.如果你想要那条水平线,它应该在xml上面手动添加LinearLayout.这将给你水平线:
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="0dp"
android:background="?android:attr/dividerVertical" />
Run Code Online (Sandbox Code Playgroud)
我做了这样的事情:
LinearLayout dialogLayout = (LinearLayout) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_addeditrecord, null);
Run Code Online (Sandbox Code Playgroud)
然后,我使用dialogLayout 调用findViewById() 来拉入按钮和其他视图并设置OnClickListeners 等...
然后显示对话框:
builder = new AlertDialog.Builder(this);
builder.setView(dialogLayout);
builder.create().show();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3666 次 |
| 最近记录: |