Reputation: 11829
I have a listview with custom rows. In the row there are these items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:focusable="false"
android:scaleType="fitXY">
</ImageView>"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
<TextView android:id="@+id/textView1"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:maxLines="1"
android:ellipsize="end">"
</TextView>
<TextView android:id="@+id/textView2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_below="@+id/textView1"
android:maxLines="1"
android:ellipsize="end">
</TextView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textsll2"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="10dp">"
<TextView android:id="@+id/textView3"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingBottom="5dp"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:background="#87EB81">
</TextView>
</LinearLayout>
</LinearLayout>
If both textviews has some content, they are centered vertically in the row (almost).
But if the lower textview is empty, I call:
holder.textView2.setVisibility(View.INVISIBLE);
holder.textView.setGravity(Gravity.CENTER_VERTICAL);
Still, the upper textview gets into the top of the row. Why?
If I set it to View.GONE, I get this, but I dont want the height of the row to change:
Upvotes: 0
Views: 809
Reputation: 109257
Update:
In a RelativeLayout
, views are aligned with their parent, with the RelativeLayout
itself, or with other views. For instance, we declared that the description is aligned with the bottom of the RelativeLayout
and that the title is positioned above the description and anchored to the parent's top. With the description GONE, RelativeLayout
doesn't know where to position the title's bottom edge. To solve this problem, you can use a very special layout parameter called layout_alignWithParentIfMissing.
This boolean
parameter simply tells RelativeLayout
to use its own edges as anchors when a constraint target is missing. For instance, if you position a view to the right of a GONE view and set alignWithParentIfMissing
to true, RelativeLayout
will instead anchor the view to its left edge. In our case, using alignWithParentIfMissing
will cause RelativeLayout
to align the title's bottom with its own bottom.
For more look at Layout Tricks: Creating Efficient Layouts
Upvotes: 0
Reputation: 427
Use android:layout_weight
to set all the childs of the main LinearLayout to mesure the same.
Use a LinearLayout as parent for textView1 and textView2. Use Use android:layout_weight
to have the same size for each one.
Define TextView one with android:gravity="center_vertical|left"
Then, if you don't want to show the second TextView, set its visibility as View.GONE
.
Maybe this will resolve your problem,
Upvotes: 1
Reputation: 7849
To Solve for your problem what u can do is have the LinearLayout fill Parent.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
And use View.GONE
This will make the Row Height Same.
If you see a gap after Best Car in World then try using Relative Layout and align last Layout to bottom.
Upvotes: 0
Reputation: 16191
Try to fix some value to your RelativeLayout's height like-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
Upvotes: 0
Reputation: 9801
Try to use:
<TextView
...
android:layout_height="?android:attr/listPreferredItemHeight"
...
/>
in your_list_item.xml. And use View.GONE
Upvotes: 0
Reputation: 629
This can be possible in your RelativeLayout
by setting up the layout_below
and layout_above
with the help of parent alignment by using layout_centerInParent
and set the value true of layout_centerHorizontal
Hope, this will work.
Upvotes: 0