2012-05-29 40 views
0

我已經嘗試LinearLayout/layout_weightRelativeLayout,但不能使佈局按預期行爲。因爲他們需要的空間或分配他們的空間或分佈他們的空間或文本,因此他們分發他們

我想要做的是一個佈局,其中兩個水平排列的TextViews和一個分隔符。 (包含)佈局具有恆定的最大寬度,但可能小於(但不大於)。兩個TextViews的寬度應該取決於它們的內容,但也應該有一個上限,以便它們全部合併到這個佈局中。

這是這兩種情況可視化:

一個)兩者TextViews配合到它們的佈局容器,並應內襯向上與之間的分隔符。下面,包含的佈局由外部支架定義,並且可能大於TextViews

[[abcdefgs][delim][12334345]    ] 

(容器)佈局寬度可以wrap_contentfill_parent

b)雙方TextViews不適合它們的佈局容器的(最大)的寬度。然後它們應該均勻分佈在其佈局中(具有最大寬度)。然後截斷TextViews的文本。

..... 50% .....   ..... 50% ..... 
[[abcdefghijk...][delim][123456789012...]] 

有人知道如何做到這一點?

回答

0

我不知道這是否是你想要的東西(你的問題是不明確),但看看這個佈局幫助:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ellipsize="end" 
      android:text="TextView" /> 

     <View 
      android:layout_width="2dp" 
      android:layout_height="fill_parent" 
      android:background="#99cc00" /> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ellipsize="end" 
      android:text="TextView" /> 
    </LinearLayout> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" /> 

</LinearLayout> 

其中兩個TextViews之間的View代表的分隔符。在兩個TextView中插入更多文字以查看他們拍攝的空間。

+0

好吧。如果Textviews佔用的空間比他們的父母少,它工作正常。隨着更多的文本,垂直空間分佈(textA | delim | textB)約爲70%| 30%。兩個(textA,textB)都被截斷,但不相等。所以它比我的版本有一個相對佈局(從左到右對齊),其中只有textB被截斷。 – Chilibeta

+0

@Chilibeta當我測試佈局時,我對「TextViews」(如'「sdfjfdsfsdfbdjsfbdjfbdsjkfbdsjf」')使用了相同的(確切的)文本,並且它工作正常(您可以嘗試並測試它)。我猜兩個具有不同寬度的'TextViews'使'layout_weight'無用。 – Luksprog

+0

@Chilibeta運行一些測試後,我認爲你不能在xml佈局文件中實現這種類型的佈局。我之所以這麼說是因爲你最終必須在某處使用'layout_weight',這會迫使你的'TextViews'在它們不應該有相同的寬度時(例如:當第二個'TextView'更大時比第一個在第一個'TextView'和divider之間會出現巨大的差距,因爲'layout_weight'會被分成50%)。我沒有測試任何東西,但你可以嘗試在代碼中安排'TextViews',但這很容易變成雜亂的東西。 – Luksprog

相關問題