我有一个在对话框主题 Activity 中使用的布局。这正确地呈现了。然而,它现在呈现在一个对话框 fragment 中,并且一些 View 彼此堆叠。我尝试在顶层添加线性布局,因为我发现一篇文章建议这样做,但这没有帮助。似乎正在跳过将项目标记为其他内容右侧或左侧的代码,我不确定为什么。任何指导或帮助表示赞赏。
android studio 渲染看起来正确:
布局文件如下
<?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">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/delete_text_overlay"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Delete_Overlay"
android:textColor="#FF000000"
android:onClick="btn_Delete_TextBox_Click"/>
<EditText
android:id="@+id/text_edit_view"
android:layout_below="@id/delete_text_overlay"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="@android:color/white"
android:background="#00000000"/>
<Button
android:id="@+id/font_Style_Button"
android:layout_below="@id/text_edit_view"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Font @amp; Style"
android:onClick="fontStyleBtnClick"/>
<TextView
android:id="@+id/center_point_text_edit"
android:layout_below="@id/font_Style_Button"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/confirm_text_edit"
android:layout_below="@id/font_Style_Button"
android:layout_toLeftOf="@id/center_point_text_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Confirm_Text_Edit_Changes"
android:onClick="btn_Confirm_Changes_Click"/>
<Button
android:id="@+id/cancel_text_edit"
android:layout_below="@id/font_Style_Button"
android:layout_toRightOf="@id/center_point_text_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Cancel_Text_Edit"
android:onClick="btn_Cancel_Changes_Click"/>
</RelativeLayout>
</LinearLayout>
在对话框 fragment 中显示布局的 Java 代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.dialog_fragment_text_properties, container, false);
}
创建显示对话框的请求的 Java 代码
void showDialog(dialogTypeEnum dialogType, Bundle tempBundle) {
mStackLevel++;
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag(dialogType.stringValue);
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment newFragment;
switch (dialogType){
case DIALOG_FRAGMENT_FONT_PROPERTIES:
newFragment = TextPropertiesDialogFragment.newInstance(tempBundle);
break;
case DIALOG_FRAGMENT_TEXT_EDIT:
newFragment = TextEditDialogFragment.newInstance(tempBundle);
break;
case DIALOG_FRAGMENT_TEXT_PROPERTIES:
//This call
newFragment = TextPropertiesDialogFragment.newInstance(tempBundle);
break;
default:
newFragment = null;
break;
}
if (newFragment != null ){
newFragment.show(ft, dialogType.stringValue);
}
}
最佳答案
我必须为此实现解决方案,因为我找不到它不起作用的原因。解决方法是将两个按钮放在相对布局内的线性布局中,并删除按钮用于对齐自身的空白 TextView 。这工作正常,但不太理想。
更改代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/delete_text_overlay"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Delete_Overlay"
android:textColor="#FF000000"
android:onClick="btn_Delete_TextBox_Click"/>
<EditText
android:id="@+id/text_edit_view"
android:layout_below="@id/delete_text_overlay"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="@android:color/white"
android:background="#00000000"/>
<Button
android:id="@+id/font_Style_Button"
android:layout_below="@id/text_edit_view"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Font @amp; Style"
android:onClick="fontStyleBtnClick"/>
<!-- <TextView
android:id="@+id/center_point_text_edit_df"
android:layout_below="@id/font_Style_Button"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "/>
<Button
android:id="@+id/confirm_text_edit"
android:layout_below="@id/font_Style_Button"
android:layout_toStartOf="@id/center_point_text_edit_df"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Confirm_Text_Edit_Changes"
android:onClick="btn_Confirm_Changes_Click"/>
<Button
android:id="@+id/cancel_text_edit"
android:layout_below="@id/font_Style_Button"
android:layout_toEndOf="@id/center_point_text_edit_df"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Cancel_Text_Edit"
android:onClick="btn_Cancel_Changes_Click"/> -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:layout_below="@id/font_Style_Button"
>
<Button
android:id="@+id/confirm_text_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confirm"
android:onClick="btn_Confirm_Changes_Click"/>
<Button
android:id="@+id/cancel_text_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:onClick="btn_Cancel_Changes_Click"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
关于android - relativelayout 和 View 与layout_toLeftOf 属性在DialogFragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37754971/