gpt4 book ai didi

android - 如何在不同屏幕尺寸的设备上显示相同尺寸的对话框

转载 作者:行者123 更新时间:2023-11-30 03:01:48 24 4
gpt4 key购买 nike

我在我的应用程序中创建了一个固定大小的对话框。当我用 240x320 像素大小的手机进行测试时,它显示完美。但它在平板电脑中显示不合适。看下面的图片

enter image description here

手机图片

enter image description here图片来自标签

我试过下面的代码

public void showDialog(){
// Create custom dialog object
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");

// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.ic_launcher);

dialog.show();
dialog.getWindow().setLayout(180, 150);

Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

}

对话框.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ImageView
android:id="@+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />

<TextView
android:id="@+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/imageDialog"/>

<Button
android:id="@+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/textDialog"
android:layout_toRightOf="@+id/imageDialog"
/>

</RelativeLayout>

如何解决这个问题?

提前致谢

最佳答案

终于找到了解决办法

我删除了 dialog.setTitle("Custom Dialog");dialog.getWindow().setLayout(180, 150); 行并添加了 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

修改后的代码如下

public void showDialog(){
// Create custom dialog object
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);

// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.ic_launcher);

dialog.show();

Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});

}

我对布局文件进行了另一次更改。将 替换为 并将行 android:layout_width="fill_parent"android:layout_height="fill_parent" 替换为 android:layout_width="180dp"android:layout_height="150dp"

修改后的布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="180dp"
android:layout_height="150dp" >

<ImageView
android:id="@+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />

<TextView
android:id="@+id/textDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />

<Button
android:id="@+id/declineButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
/>

</LinearLayout>

关于android - 如何在不同屏幕尺寸的设备上显示相同尺寸的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22402585/

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