2015-03-19 73 views
1

我有一個佈局,我試圖有一個視圖填充所有剩餘的空間,除了最後一些50dp寬度的圖像。當我將第一個視圖的寬度設置爲match_parent時,它佔據了整個視圖,並且無論我設置爲圖像寬度,它都放置在屏幕邊界之外。這是發生了什麼:如何使Android視圖寬度=(父寬度 - 一些常量)

enter image description here

但我想它是這樣的:(第一視圖的寬度設置爲常數實現只是爲了演示,這是我絕對不希望,因爲它應該填一個動態量寬度)

enter image description here

這是我目前的佈局:

enter image description here

問題出在LinearLayout (vertical),它剛好在層次結構中的頭像視圖下方。它佔據了正確的空間,但兒童卻沒有按照我想要的方式行事。

我該如何達到所需的佈局?

+0

你是如何設計你的佈局?你可以使用wifghts?張貼布局 – KOTIOS 2015-03-19 12:46:24

+0

您使用了什麼類型的佈局? – CAS 2015-03-19 12:48:52

+0

@Diva我已經更新了這個問題。 – 2015-03-19 12:55:48

回答

2

看看這個例子。主要想法是爲需要佔用所有可用空間的視圖設置android:layout_width="0dp"android:layout_weight="1"。在我的情況下,它是TextView在你的情況下,它可以是另一種佈局或其他任何東西。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     .... 
     /> 
    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     .... 
     /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ... 
     /> 

</LinearLayout> 

更新

更多信息可以在這裏找到question

+0

它的工作!設置textview的layout_weight爲1完全正是我需要的。感謝這個例子! – 2015-03-19 13:00:59

1

使用RelativeLayout進行佈局,並將右側的ImageView的中心TextView元素屬性設置爲to_rightof。無論圖像的大小如何,textview都會自動調整其寬度。

相關問題