我有一個自定義對話框設置和我的佈局的寬度設置爲wrap_content
但它仍然採取整個屏幕寬度。我甚至嘗試將寬度設置爲100dp並且它仍然使用整個屏幕爲什麼會發生這種情況?Android對話框wrap_content不工作
我已經建立了對話是這樣的:
public class DialogGoToLine extends Dialog
{
//The root view of the dialog
private static View rootView;
public DialogGoToLine(Context context, int themeResId){
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Allow the dialog to be canceled by tapping out of its bounds
setCancelable(true);
//Inflate the custom layout
LayoutInflater layoutInflater = LayoutInflater.from(HotspotHelper.getAppContext());
rootView = layoutInflater.inflate(R.layout.dialog_go_to_line, null);
//Set up the layout
//setTitle(HotspotHelper.getAppContext().getString(R.string.clientDetails));
//setTitle("Go to line");
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(rootView);
final EditText editText = (EditText) rootView.findViewById(R.id.editTextGoToLine);
Button buttonGoToLine = (Button) rootView.findViewById(R.id.buttonGoToLine);
buttonGoToLine.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
int line = Integer.valueOf(editText.getText().toString()) - 1;
((LinearLayoutManager)LogActivity.recyclerViewLog.getLayoutManager()).scrollToPositionWithOffset(line, (int)HotspotHelper.dpToPx(30));
dismiss();
}
});
}
}
而且佈局XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/textViewGoToLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:labelFor="@+id/editTextGoToLine"
android:text="Go to line: "
android:background="@color/accent"
android:textColor="@color/primaryText"
android:textSize="18sp"/>
<EditText
android:id="@id/editTextGoToLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_margin="4dp"
android:layout_centerVertical="true"
android:inputType="number"
android:gravity="center"
android:textSize="18sp"/>
<Button
android:id="@+id/buttonGoToLine"
android:layout_width="wrap_content"
android:text="Go"
android:layout_margin="4dp"
android:textSize="18sp"
android:layout_height="wrap_content"/>
艾迪T 1 如果它的確與衆不同,這個對話框有在我宣佈背景這繪製風格:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/colorPrimary"/> <corners android:radius="2dp" /> </shape>
遠射:我在Studio的即時運行中遇到了與對話相關的錯誤。如果你正在使用它,你可以嘗試卸載應用程序並重試? – Shaishav
你的父母linear_layout寬度是100dp – vinoth12594
@ Shaishav我目前在我的手機上沒有即時運行的AIDE – TychoTheTaco