// try this way
<?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="wrap_content"
android:gravity="center"
android:padding="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textViewPrompt"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:paddingRight="5dp"
android:gravity="left"
android:text="Prompt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16sp" />
<TextView
android:id="@+id/textViewAnswer"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:paddingLeft="5dp"
android:text="Answer"
android:gravity="right"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutSeparator"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_marginTop="5dp"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
RelativeLayout breaking strangely when used with match_parent or wrap_content
-
09-10-2022 - |
문제
I'm trying to have two TextViews separated in the center, with one on the left side and one on the right side. This breaks in a strange way when the RelativeLayout width is set to match_parent or wrap_content, however. When the RelativeLayout is set to a manual value (e.g. 200dp), it works.
Note that both layouts preview properly in Eclipse, they just don't work correctly on a phone or an emulator.
Here is the RelativeLayout code with match_parent:
<?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="?android:attr/listPreferredItemHeight"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutSeparator"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@color/transparent"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="@+id/textViewPrompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/linearLayoutSeparator"
android:ellipsize="end"
android:maxLines="2"
android:paddingRight="5dp"
android:text="Prompt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/default_text"
android:textSize="16sp" />
<TextView
android:id="@+id/textViewAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayoutSeparator"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:maxLines="2"
android:paddingLeft="5dp"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/default_text"
android:textSize="16sp" />
</RelativeLayout>
And here is the result:
And here is the code with a manual width value (200dp, in this case), which creates a proper result:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayoutSeparator"
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@color/transparent"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:id="@+id/textViewPrompt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/linearLayoutSeparator"
android:ellipsize="end"
android:maxLines="2"
android:paddingRight="5dp"
android:text="Prompt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/default_text"
android:textSize="16sp" />
<TextView
android:id="@+id/textViewAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayoutSeparator"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ellipsize="end"
android:maxLines="2"
android:paddingLeft="5dp"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/default_text"
android:textSize="16sp" />
</RelativeLayout>
Here is the result:
The only difference between the two layouts is the line:
android:layout_width="200dp"
Does anyone know what the issue could be? Thanks in advance for your help!
해결책
제휴하지 않습니다 StackOverflow