简体   繁体   中英

android layout of relativelayout in a dialog

It may seem a little stupid but I just cannot figure out why.

The view I expect is as below. I want TextView A to be center horizontal while TextView B alignLeft of A and above A.

在此处输入图片说明

when i try declare B after A (using layout_alignleft and layout_above in B)in the layout xml then TextView B cannot show and findviewId method becomes chaos. if i try declare B before A (using layout_alignleft in B,using layout_below in A ), then as u can guess TextView B just aligns to the left of the whole view rather than TextView A.

This happens in a DIALOG view.Of course I know how to display it in a activity...When it comes to dialog view just seems absurd

So here comes the question,am i stupid?

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

android:layout_toLeftOf="@+id/txtA"

Try like this, this works for me:

Do this while showing dialog,

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

or, you can fix dialog height like:

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

or android:layout_height="300dp"

check sample code here .

This is exactly like the picture:

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

check the following code

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

Change your dialog layout like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM