【问题标题】:RelativeLayout and view wiith layout_toLeftOf attribute not working in DialogFragment具有 layout_toLeftOf 属性的 RelativeLayout 和视图在 DialogFragment 中不起作用
【发布时间】:2023-04-08 17:37:01
【问题描述】:

我有一个用于对话框主题活动的布局。这正确地呈现。但是,它现在呈现在一个对话框片段中,并且一些视图彼此堆叠。我尝试在顶层添加线性布局,因为我发现了一个建议它的帖子,但这没有帮助。似乎正在跳过将项目标记到其他东西的右侧或左侧的代码,我不知道为什么。任何指导或帮助表示赞赏。

手机布局如下:

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>

在对话框片段中显示布局的 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);
    }
}

【问题讨论】:

  • 我在文本中心点文本视图中添加了一个空格,但这没有任何区别。我还尝试将属性 toleftof 和 torightof 更改为 startof 和 toendof。

标签: android layout layout-inflater dialogfragment


【解决方案1】:

我不得不解决这个问题,因为我找不到它不起作用的原因。解决方法是将两个按钮放在相对布局内的线性布局中,并删除按钮用于对齐的空白文本视图。这工作正常,但不太理想。

如下修改代码

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

【讨论】:

  • 今天遇到类似问题,想知道还有没有更好的解决办法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-19
  • 2012-10-18
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 2011-06-21
相关资源
最近更新 更多