【发布时间】:2012-05-13 02:36:44
【问题描述】:
之前有人问过这个问题:AlertDialog custom title has black border
但没有得到满意的回答 - 并且缺少一些信息。
我正在尝试在 Android 中创建一个没有标题且底部没有任何按钮的自定义对话框。
但是,生成的对话框在视图的顶部和底部有黑色“边框”/“间距”/一些东西。
使用 Dialog 基类创建的对话框必须有标题。如果你 不要调用 setTitle(),那么用于标题的空间仍然存在 空的,但仍然可见。如果你根本不想要一个标题,那么你 应该使用 AlertDialog 类创建您的自定义对话框。然而, 因为使用 AlertDialog.Builder 最容易创建 AlertDialog 类,您无权访问使用的 setContentView(int) 方法 更多。相反,您必须使用 setView(View)。这个方法接受一个视图 对象,因此您需要从 XML 扩展布局的根视图对象。
所以,这就是我所做的:
Welcome.java
public class Welcome extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.welcomedialog, (ViewGroup)findViewById(R.id.layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.create().show();
}
}
welcomedialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/texturebg"
android:id="@+id/layout_root"
android:orientation="vertical"
android:padding="40px">
...
</LinearLayout>
注意:根据我在某处找到的建议,我尝试使用 FrameLayout 作为根 ViewGroup 而不是 LinearLayout - 但这没有帮助。
结果
setBackgroundDrawable 建议
public class Welcome extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.welcomedialog, (ViewGroup)findViewById(R.id.layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
AlertDialog dialog = builder.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.show();
}
}
不适合我。
【问题讨论】:
-
这段代码可以使用 setPositiveButton 方法吗?如果我在布局中创建的新视图有按钮,如何设置 onClickListener 回调?
-
@Fakebear,我认为您的评论/问题超出了这个问题的范围。您可能想要搜索该问题和/或提出一个新问题。
标签: android android-layout android-dialog