AlertDialog:如何删除视图上方和下方的黑色边框 [英] AlertDialog: How To Remove Black Borders Above and Below View
问题描述
之前有人问过这个问题: 你会看到大部分方法只是围绕 的代理方法(外观)private AlertController mAlert
.
查看AlertController 类源 你会看到4个有趣的成员变量:
private int mViewSpacingLeft;私人 int mViewSpacingTop;私人 int mViewSpacingRight;私人 int mViewSpacingBottom;私有布尔 mViewSpacingSpecified = false;
将 mViewSpacingSpecified
设置为 true
将移除对话框顶部和底部的边框.
这可以通过更改此行正确完成:
dialog.setView(layout);
到:
dialog.setView(layout, 0, 0, 0, 0);
This question has been asked before: AlertDialog custom title has black border
But was not answered satisfactorily - and is missing some information.
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.
From the Documentation:
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>
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.
Result
setBackgroundDrawable Suggestion
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.
If you look at the AlertDialog class source you'll see most of the methods are simply proxy methods (facade) around private AlertController mAlert
.
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;
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);
to:
dialog.setView(layout, 0, 0, 0, 0);
这篇关于AlertDialog:如何删除视图上方和下方的黑色边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!