簡體   English   中英

Android TextView寬度與ImageView

[英]Android TextView width with ImageView

我有一個帶有TextView和ImageView的ListView單元格。 我希望ImageView保持在右側,而TextView寬度應為“單元格寬度減去ImageView占用的所有空間”。 現在我正在使用這個:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="@dimen/defaultCellHeight">

        <TextView
            android:id="@+id/randomSettingShow"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/defaultCellHeight"
            android:paddingLeft="30dp"
            android:textSize="@dimen/defaultTextSize"
            android:focusable="false"
            android:textStyle="normal"
            android:gravity="center_vertical|left"
            android:layout_alignParentLeft="true" />

         <ImageView
            android:id="@+id/randomSettingShowCheck"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/defaultCellHeight"
            android:gravity="center"
            android:layout_marginRight="15dp"
            android:layout_alignParentRight="true"
            android:contentDescription="@string/checkmark" />
</RelativeLayout>

但是問題是,如果TextView中的文本足夠長,則該文本將進入ImageView下面,這顯然是錯誤的。 我希望TextView的最大寬度設置為ImageView的左邊框。

我怎樣才能做到這一點?

您可以嘗試將TextView與可繪制的復合集一起使用- 請參閱文檔

您可以嘗試如下操作:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_toLeftOf="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"
        android:id="@+id/textView"
        android:layout_alignParentLeft="true" />

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true" />
</RelativeLayout>

就像Asahi所說的那樣,復合可繪制對象可能會更適合您的情況。 這里看看這個答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM