[英]Set TextView width equal to ImageView width within LinearLayout
我得到以下代碼:
<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的大小的原因。)
該代碼段導致:
|-------------------|
| | | |
| | IMAGE | |
| | | |
|-------------------|
| |
| <<< TEXT >>> |
| |
|-------------------|
我想得到什么:
|-------------------|
| | | |
| | IMAGE | |
| | | |
|-------------------|
| | | |
| | TEXT | |
| | | |
|-------------------|
我不確定是否可以使用純XML格式,或者是否必須為此使用Java。
無論如何-我不能使用RelativeLayout的layout_alignLeft
和layout_alignRight
因為我必須先設置ImageView的高度。
任何建議將不勝感激!
您可以將RelativeLayout用作父級,並在textview中放置以下幾行
android:layout_below="@+id/icon"
android:layout_alignLeft="@+id/icon"
android:layout_alignRight="@+id/icon"
從您的問題中我了解到的是,您希望您的ImageView
和TextView
具有相同的寬度。
有兩種方法可以實現這一目標。
通過XML :在XML文件中的ImageView
設置scaleType
屬性中,
android:scaleType="fitXY"
這樣可以確保您的圖像適合ImageView
,並且ImageView
寬度不會變化。
通過Java:圖像設置后ImageView
得到的尺寸ImageView
,並設置相同的尺寸,你TextView
。
嘗試這個,
<?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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.