gpt4 book ai didi

android - 设置自定义对话框的高度和宽度

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:02 25 4
gpt4 key购买 nike

我正在制作一个自定义对话框,我希望其高度和宽度默认适合所有屏幕尺寸。

但事实并非如此。虽然对话框看起来很小。这是对话框的 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D24379"
android:orientation="vertical" >

<TextView
android:id="@+id/txt_dia"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="@android:color/white"
android:textSize="15dp"
android:textStyle="bold" >
</TextView>

<LinearLayout
android:id="@+id/layButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal" >

<Button
android:id="@+id/yesButton"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="#ffffff"
android:text="@string/yes" />

<Button
android:id="@+id/noButton"
android:layout_marginLeft="5dp"
android:layout_width="0.0dip"
android:background="#ffffff"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="@string/no" />
</LinearLayout>

这是我的对话类

public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {

public Activity c;
public Dialog d;
public Button yes, no;
public TextView content;

public CustomDialogClass(Activity a) {
super(a);
this.c = a;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (Button) findViewById(R.id.yesButton);
no = (Button) findViewById(R.id.noButton);
content=(TextView)findViewById(R.id.txt_dia);
yes.setOnClickListener(this);
no.setOnClickListener(this);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.yesButton:

break;
case R.id.noButton:
dismiss();
break;
default:
break;
}
dismiss();
}
}

如何将高度和宽度设置为默认大小,使其在所有屏幕尺寸上都能完美显示。

我正在获取一个非常小的对话框。

最佳答案

不使用对话框类,试试这个类:

    public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


ShowDialog();

}

private void ShowDialog() {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(context,
android.R.style.Theme_Translucent_NoTitleBar);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;

dialog.getWindow().setAttributes(lp);

Button yes = (Button) dialog.findViewById(R.id.yesButton);
yes.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

dialog.dismiss();

}
});

Button no = (Button) dialog.findViewById(R.id.noButton);
no.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,

dialog.dismiss();

}
});

TextView content =(TextView) dialog.findViewById(R.id.txt_dia);



dialog.show();
}
}

关于android - 设置自定义对话框的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24264630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com