AlertDialog:如何删除黑色边框上方和下方查看 [英] AlertDialog: How To Remove Black Borders Above and Below View
问题描述
这个问题已经被问过:<一href="http://stackoverflow.com/questions/10222241/alertdialog-custom-title-has-black-border">AlertDialog自定义标题有黑色边框
This question has been asked before: AlertDialog custom title has black border
但没有得到回答令人满意 - 和丢失一些信息
But was not answered satisfactorily - and is missing some information.
我试图创建的Android定制对话框没有标题,没有沿底部的任何按钮。
I'm trying to create a custom dialog in Android without a title and without any buttons along the bottom.
不过,在出现的对话框有黑色的边界/间距/某事沿视图的顶部和底部。
However, the resulting dialog has black "borders"/"spacing"/something along the top and bottom of the view.
从文档:
与基地对话框类制成的对话框必须有一个标题。如果你 不叫的setTitle(),然后用于标题的空间保持 空,但仍然可见。如果你不想要一个标题,那么你 要创建一个使用AlertDialog类的自定义对话框。然而, 因为AlertDialog创建最简单的AlertDialog.Builder 一流的,你没有访问使用的setContentView(int)方法 以上。相反,你必须使用的setView(查看)。此方法接受一个视图 对象,所以你需要从充气XML布局的根查看对象。
A dialog made with the base Dialog class must have a title. If you don't call setTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using the AlertDialog class. However, because an AlertDialog is created easiest with the AlertDialog.Builder class, you do not have access to the setContentView(int) method used above. Instead, you must use setView(View). This method accepts a View object, so you need to inflate the layout's root View object from XML.
所以,这就是我所做的:
So, that's what I did:
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
为每一个建议,我发现的地方 - 但这并没有帮助。
NOTE: I've tried using FrameLayout
as the root ViewGroup
instead of LinearLayout
as per a suggestion I found somewhere - but that didn't help.
结果
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();
}
}
没为我工作。
Didn't work for me.
推荐答案
如果你看一下<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/app/AlertDialog.java">AlertDialog类源你会看到大部分的方法都是简单的代理方法(正面)左右私人AlertController mAlert
。
If you look at the AlertDialog class source you'll see most of the methods are simply proxy methods (facade) around private AlertController mAlert
.
纵观<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/com/android/internal/app/AlertController.java#AlertController">AlertController类源你会看到4有趣的成员变量:
Looking at the AlertController class source you'll see 4 interesting member variables:
private int mViewSpacingLeft;
private int mViewSpacingTop;
private int mViewSpacingRight;
private int mViewSpacingBottom;
private boolean mViewSpacingSpecified = false;
设置 mViewSpacingSpecified
到真
将删除在对话框的顶部和底部的边框。
Setting mViewSpacingSpecified
to true
will remove the borders on the top and bottom of the dialog.
这是通过改变这一行得当:
This is done properly by changing this line:
dialog.setView(layout);
到
dialog.setView(layout, 0, 0, 0, 0);
这篇关于AlertDialog:如何删除黑色边框上方和下方查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!