Android 对话框被压缩
Android dialog is compressed
我正在使用布局来显示对话框。在我的 xml 设计中它是完美的,但是当我在应用程序中打开它时,它被打乱了。下面是屏幕截图:
xml文件设计:
以及手机中的对话框:
我的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="match_parent"
android:orientation="vertical"
android:weightSum="11"
android:background="#ffffff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TextView
android:id="@+id/textView_goal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Set You Goal with "
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4" >
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1.5"
android:layout_height="match_parent" >
<EditText
android:id="@+id/goal_value_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:layout_marginLeft="40dp"
android:inputType="phone" >
<requestFocus />
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent" >
<TextView
android:id="@+id/goal_unit_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="mm Hg"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="2" >
</LinearLayout>
<LinearLayout
android:id="@+id/hide_lay"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1.5"
android:layout_height="match_parent" >
<EditText
android:id="@+id/goal_value_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_marginLeft="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="mm Hg"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="2" >
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button_goal_set"
android:layout_width="match_parent"
android:layout_marginLeft="40dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Set"
android:textColor="#ffffff"
android:background="#6FD0EA" />
</RelativeLayout>
</LinearLayout>
我的 java 代码是:
final Dialog d = new Dialog(AddMeasurements.this);
//tell the Dialog to use the dialog.xml as it's layout description
d.setContentView(R.layout.measurement_goal);
d.setTitle("Set Goal");
final EditText value = (EditText) d.findViewById(R.id.goal_value_1);
final EditText value2 = (EditText) d.findViewById(R.id.goal_value_2);
goal=(TextView)d.findViewById(R.id.textView_goal);
goal.setText("Set your goal for "+getIntent().getStringExtra("mname"));
LinearLayout l=(LinearLayout)d.findViewById(R.id.hide_lay);
l.setVisibility(View.GONE);
final TextView unit=(TextView)d.findViewById(R.id.goal_unit_1);
unit.setText("mg/dl");
Button send = (Button) d.findViewById(R.id.button_goal_set);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
sp.edit().putString("Blood Glucose", value.getText().toString()+" "+unit.getText().toString()).commit();
d.dismiss();
}
});
d.show();
我试过所有方法,但无法修复此对话框,请帮忙
谢谢
尝试将您的线性布局编辑为此
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="11"
android:background="#ffffff">
并尝试编辑其他视图元素的所有高度和宽度 属性 不应该依赖于父对话布局的地方。
并在你的对话代码中添加这一行
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
用下面的代码更改您的对话框代码,它肯定会工作
final Dialog d = new Dialog(MainActivity.this);
//tell the Dialog to use the dialog.xml as it's layout description
d.setContentView(R.layout.dialog);
d.setTitle("Set Goal");
//Added this lines
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
final EditText value = (EditText) d.findViewById(R.id.goal_value_1);
final EditText value2 = (EditText) d.findViewById(R.id.goal_value_2);
TextView goal=(TextView)d.findViewById(R.id.textView_goal);
goal.setText("Set your goal for "+getIntent().getStringExtra("mname"));
LinearLayout l=(LinearLayout)d.findViewById(R.id.hide_lay);
l.setVisibility(View.GONE);
final TextView unit=(TextView)d.findViewById(R.id.goal_unit_1);
unit.setText("mg/dl");
Button send = (Button) d.findViewById(R.id.button_goal_set);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// sp.edit().putString("Blood Glucose", value.getText().toString()+" "+unit.getText().toString()).commit();
d.dismiss();
}
});
d.show();
//added this line also
d.getWindow().setAttributes(lp);
我正在使用布局来显示对话框。在我的 xml 设计中它是完美的,但是当我在应用程序中打开它时,它被打乱了。下面是屏幕截图:
xml文件设计:
以及手机中的对话框:
我的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="match_parent"
android:orientation="vertical"
android:weightSum="11"
android:background="#ffffff">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TextView
android:id="@+id/textView_goal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Set You Goal with "
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4" >
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1.5"
android:layout_height="match_parent" >
<EditText
android:id="@+id/goal_value_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:layout_marginLeft="40dp"
android:inputType="phone" >
<requestFocus />
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent" >
<TextView
android:id="@+id/goal_unit_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="mm Hg"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="2" >
</LinearLayout>
<LinearLayout
android:id="@+id/hide_lay"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1.5"
android:layout_height="match_parent" >
<EditText
android:id="@+id/goal_value_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:layout_marginLeft="40dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="mm Hg"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:weightSum="2" >
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="@+id/button_goal_set"
android:layout_width="match_parent"
android:layout_marginLeft="40dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Set"
android:textColor="#ffffff"
android:background="#6FD0EA" />
</RelativeLayout>
</LinearLayout>
我的 java 代码是:
final Dialog d = new Dialog(AddMeasurements.this);
//tell the Dialog to use the dialog.xml as it's layout description
d.setContentView(R.layout.measurement_goal);
d.setTitle("Set Goal");
final EditText value = (EditText) d.findViewById(R.id.goal_value_1);
final EditText value2 = (EditText) d.findViewById(R.id.goal_value_2);
goal=(TextView)d.findViewById(R.id.textView_goal);
goal.setText("Set your goal for "+getIntent().getStringExtra("mname"));
LinearLayout l=(LinearLayout)d.findViewById(R.id.hide_lay);
l.setVisibility(View.GONE);
final TextView unit=(TextView)d.findViewById(R.id.goal_unit_1);
unit.setText("mg/dl");
Button send = (Button) d.findViewById(R.id.button_goal_set);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
sp.edit().putString("Blood Glucose", value.getText().toString()+" "+unit.getText().toString()).commit();
d.dismiss();
}
});
d.show();
我试过所有方法,但无法修复此对话框,请帮忙 谢谢
尝试将您的线性布局编辑为此
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="11"
android:background="#ffffff">
并尝试编辑其他视图元素的所有高度和宽度 属性 不应该依赖于父对话布局的地方。
并在你的对话代码中添加这一行
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
用下面的代码更改您的对话框代码,它肯定会工作
final Dialog d = new Dialog(MainActivity.this);
//tell the Dialog to use the dialog.xml as it's layout description
d.setContentView(R.layout.dialog);
d.setTitle("Set Goal");
//Added this lines
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
final EditText value = (EditText) d.findViewById(R.id.goal_value_1);
final EditText value2 = (EditText) d.findViewById(R.id.goal_value_2);
TextView goal=(TextView)d.findViewById(R.id.textView_goal);
goal.setText("Set your goal for "+getIntent().getStringExtra("mname"));
LinearLayout l=(LinearLayout)d.findViewById(R.id.hide_lay);
l.setVisibility(View.GONE);
final TextView unit=(TextView)d.findViewById(R.id.goal_unit_1);
unit.setText("mg/dl");
Button send = (Button) d.findViewById(R.id.button_goal_set);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// sp.edit().putString("Blood Glucose", value.getText().toString()+" "+unit.getText().toString()).commit();
d.dismiss();
}
});
d.show();
//added this line also
d.getWindow().setAttributes(lp);