Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对话框中relativelayout的android布局_Android_Android Layout_Dialog - Fatal编程技术网

对话框中relativelayout的android布局

对话框中relativelayout的android布局,android,android-layout,dialog,Android,Android Layout,Dialog,这似乎有点愚蠢,但我就是不明白为什么 我期望的观点如下。我希望TextView A居中水平,而TextView B在A的左侧和A的上方对齐 当我尝试在布局xml中的A之后声明B时(使用layout_alignleft和B中的layout_),TextView B无法显示,FindViewWid方法变得混乱。 如果我尝试在A之前声明B(在B中使用layout_alignleft,在A中使用layout_),那么正如你所猜测的那样,TextView B只是对齐到整个视图的左侧,而不是TextVie

这似乎有点愚蠢,但我就是不明白为什么

我期望的观点如下。我希望TextView A居中水平,而TextView B在A的左侧和A的上方对齐

当我尝试在布局xml中的A之后声明B时(使用layout_alignleft和B中的layout_),TextView B无法显示,FindViewWid方法变得混乱。 如果我尝试在A之前声明B(在B中使用layout_alignleft,在A中使用layout_),那么正如你所猜测的那样,TextView B只是对齐到整个视图的左侧,而不是TextView A

这是在对话视图中发生的。当然我知道如何在活动中显示它…当涉及到对话视图时,它看起来很荒谬


所以问题来了,我笨吗?

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

android:layout_toLeftOf="@+id/txtA"

这与图片一模一样:

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

检查以下代码

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


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

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

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\u height=“300dp”


检查示例代码。

如下更改对话框布局:

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


不,你不傻。你只是没怎么试过。我建议使用线性布局,而不是Relativelayout@ZeeshanShabbir但我怎样才能得到“一个左对齐另一个”的效果呢?太困惑了…不要这样提问题…至少在你把问题贴在这里之前要花点功夫…你可以试着把这个应用到对话中,然后你会发现一些有趣的东西。这正是Confuss meIt的魅力所在。顺便说一句,你知道为什么“setlayout”有效以及为什么普通技巧失败吗?真的想解释一下吗?lolDo你发现setlayout方法的两个参数之一必须是MATCH_PARENT,这样alignleft才能有效。但是对话框将另外两个宽或高,无论如何包装内容?我张贴了一个以上的答案,尝试通过改变你的对话框布局这样。现在不需要在活动中使用“setLayout”方法。已回复urs,您的新技巧必须依赖于中心文本视图比alignLeft视图长。Lol。不管怎样,谢谢:)在这种情况下,这两个文本视图的长度将使其分为两种。LOLAs根据我的知识,在这种情况下,您应该将该高度设置为“匹配父对象”或固定对话框高度。如果您保留换行内容,则主布局将首先呈现,文本视图居中,因此,即使您提供该文本视图上的其他依赖项,这些依赖项也无法在相对视图的情况下查看。