android - relativelayout 和 View 与layout_toLeftOf 属性在DialogFragment 中不起作用

标签 android layout layout-inflater dialogfragment

我有一个在对话框主题 Activity 中使用的布局。这正确地呈现了。然而,它现在呈现在一个对话框 fragment 中,并且一些 View 彼此堆叠。我尝试在顶层添加线性布局,因为我发现一篇文章建议这样做,但这没有帮助。似乎正在跳过将项目标记为其他内容右侧或左侧的代码,我不确定为什么。任何指导或帮助表示赞赏。

手机布局如下所示: Mobile Render

android studio 渲染看起来正确:

Android Studio Render

布局文件如下

<?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/

相关文章:

android mapview - 附加到类似 "onZoom"的东西

android - 关于android服务内存不足

java - 为什么我的 java 应用程序模糊?

android - 如何解析 Android 布局 xml 文件以创建帮助页面的副本?

java - 在 onCreate() 方法运行之前命名 View 时出错

java - 将 Toast 定位在屏幕上单击的任何位置

Android格式化xml字符串丢失html标签

java - 选项卡布局实现上的 NullPointerException

HTML & CSS Div 类未加入

android - 膨胀 listView onClick 返回 null