【问题标题】:Set TextView width equal to ImageView width within LinearLayout在 LinearLayout 中设置 TextView 宽度等于 ImageView 宽度
【发布时间】:2016-04-01 17:51:59
【问题描述】:

我得到以下代码:

<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"/>

(因为我还不知道图片的大小,所以我必须通过layout_weight来设置ImageView的大小。)
代码 sn -p 的结果是:

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

我想得到什么:

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

我不确定它是否可以在纯 XML 中使用,或者我是否必须为此使用 Java。

无论如何-我不能使用RelativeLayout的layout_alignLeft和layout_alignRight,因为我必须先设置ImageView的高度。
任何建议将不胜感激!

【问题讨论】:

  • 布局中的ImageVIew 占据了父LinearLayout 的整个宽度,不管那是什么。如果图像内容没有填满整个宽度,它可能不会这样显示,但视图本身仍然与父视图一样宽。
  • 这是真的,但在这种情况下,有没有办法将父母的宽度设置为等于 ImageView 的宽度?还是我通常有错误的方法?
  • 我想不出任何只使用框架提供的布局的东西。
  • 似乎我必须在活动 onCreate() 中以编程方式设置宽度

标签: android xml android-layout layout android-linearlayout


【解决方案1】:

您可以使用 RelativeLayout 作为父级,只需将以下几行放入 textview

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

【讨论】:

    【解决方案2】:

    试试这个,

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

    【讨论】:

      【解决方案3】:

      我从您的问题中了解到,您希望 ImageView 和 TextView 具有相同的宽度。

      有两种方法可以实现。

      1. 通过 XML:在您的 ImageView 的 XML 文件中设置 scaleType 属性,

        android:scaleType="fitXY"

        这将确保您的图片适合您的ImageView 和ImageView 宽度不会变化。

      2. 通过 JAVA:将图像设置为 ImageView 后,获取 ImageView 的尺寸并将相同的尺寸设置为您的 TextView。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        相关资源
        最近更新 更多