简体   繁体   中英

Set TextView width equal to ImageView width within LinearLayout

I got the following code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:weightSum="1">

<ImageView
    android:id="@+id/slider_image"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.6"/>

<TextView
    style="@style/DefaultText"
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.4"/>

(Because I don't know the size of the image yet, thats why I have to set the size of the ImageView via layout_weight .)
The code snippet results in:

|-------------------|
|     |       |     |
|     | IMAGE |     |
|     |       |     |
|-------------------|
|                   |
|   <<<  TEXT  >>>  |
|                   |
|-------------------|

What I want to get:

|-------------------|
|     |       |     |
|     | IMAGE |     |
|     |       |     |
|-------------------|
|     |       |     |
|     | TEXT  |     |
|     |       |     |
|-------------------|

I am not sure if it's possible in plain XML or if I have to use Java therefor.

Anyways - I can't make use of RelativeLayout's layout_alignLeft and layout_alignRight because I have to set the ImageView height first.
Any suggestions would be greatly appreciated!

You can use a RelativeLayout as parent and just put the following lines for the textview

    android:layout_below="@+id/icon"
    android:layout_alignLeft="@+id/icon"
    android:layout_alignRight="@+id/icon"

What I understand from your question is that, you want your ImageView & TextView of same width.

There are two ways to achieve that.

  1. Through XML : In your XML file for your ImageView set scaleType property,

    android:scaleType="fitXY"

    this will ensure that your image will fit into your ImageView & ImageView width will not vary.

  2. Through JAVA : After setting the image to ImageView get dimensions of ImageView and set same dimensions to your TextView .

Try this,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:weightSum="1">

    <ImageView
        android:id="@+id/slider_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher"
        android:layout_weight="0.6" />

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:gravity="center"
        android:text="Textview"
        android:layout_height="0dp"
        android:layout_weight="0.4" />
</LinearLayout>

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