【问题标题】:android layout of relativelayout in a dialog对话框中相对布局的android布局
【发布时间】:2015-12-23 03:26:57
【问题描述】:

这可能看起来有点愚蠢,但我就是不知道为什么。

我期望的视图如下。我希望 TextView A 居中水平,而 TextView B alignLeft of A 和 A 以上。

当我尝试在布局 xml 中在 A 之后声明 B(在 B 中使用 layout_alignleft 和 layout_above)时,TextView B 无法显示并且 findviewId 方法变得混乱。 如果我尝试在 A 之前声明 B(在 B 中使用 layout_alignleft,在 A 中使用 layout_below),那么您可以猜到 TextView B 仅与整个视图的左侧对齐,而不是 TextView A。

这发生在 DIALOG 视图中。当然我知道如何在活动中显示它...当涉及到对话框视图时,这似乎很荒谬

那么问题来了,我傻吗?

【问题讨论】:

  • 不,你并不愚蠢。你只是没有尝试太多。我建议使用线性布局而不是相对布局
  • @ZeeshanShabbir 但我怎样才能获得“一个左对齐”的效果?太糊涂了……
  • 不要像那样提出问题......至少在你在这里发布问题之前付出一些努力......

标签: android android-layout dialog


【解决方案1】:

试试这样,这对我有用:

在显示对话框时执行此操作,

Dialog dlg = new Dialog(this);
dlg.setContentView(R.layout.dialog_layout);
dlg.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

或者,您可以像这样修复对话框高度:

dlg.getWindow().setLayout(LayoutParams.WRAP_CONTENT, 450);

android:layout_height="300dp"

查看示例代码here

【讨论】:

  • 它确实像一个魅力。顺便说一句,你知道为什么“setlayout”有效以及为什么正常的技巧会失败吗?真的想要一个解释哈哈
  • 你是否发现 setLayout 方法的两个参数之一必须是 MATCH_PARENT 才能使 alignleft 工作。但是对话框会另外两个宽还是高,反正要换行内容?
  • 我又发布了一个答案,试着改变你的对话框布局。现在无需在您的活动中使用“setLayout”方法。
  • 已经回复了 urs,你的新技巧必须依赖于中心 TextView 比 alignLeft 长。Lol。不管怎样,谢谢:)
【解决方案2】:

您需要使用 toLeftOf 属性来声明 TextView B。

android:layout_toLeftOf="@+id/txtA"

【讨论】:

    【解决方案3】:

    这和图片一模一样:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView B align left of A"
            android:textSize="20dp"
            android:layout_above="@+id/textView2"
            android:layout_alignStart="@+id/textView2"
            android:layout_alignParentEnd="true" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="TextView A"
            android:textSize="20dp"
            android:id="@+id/textView2" />
    
    </RelativeLayout>
    

    【讨论】:

    • 你可以尝试将它应用到一个对话框然后你会发现一些有趣的东西。这正是让我困惑的地方
    【解决方案4】:

    检查以下代码

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEEEEE">
    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/textView1"
        android:text="HAI"
        android:layout_centerHorizontal="true"/>
    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:id="@+id/textView2"
        android:text="HELLO"
        android:layout_marginStart="100dp"
        android:layout_toStartOf="@id/textView1"/>
    </RelativeLayout>
    

    【讨论】:

      【解决方案5】:

      像这样改变你的对话框布局:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:orientation="vertical"
          android:padding="30dp" >
      
          <TextView
              android:id="@+id/sample2"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="TextView B align left of A" />
      
          <TextView
              android:id="@+id/sample"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="TextView A" />
      </LinearLayout>
      </RelativeLayout>
      

      【讨论】:

      • 这种情况下这两个TextView的长度会使其分为两种。哈哈
      • 据我所知,在这种情况下,您应该将该高度设为“match_parent”或修复对话框高度。
      • 如果你保留wrap_content,主布局会先渲染,textview居中,所以即使你给其他依赖于那个textview,那些在RelativeLayout的情况下也无法查看。
      【解决方案6】:

      似乎 alignLeft 或 alignStart 在对话框中不起作用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        • 2013-10-09
        • 1970-01-01
        相关资源
        最近更新 更多