2013-10-09 101 views
0

我有一個簡單的XML我試圖膨脹,並與佈局掙扎。我想要實現的是非常簡單的(或者我認爲),所以我確信我錯誤地做了這件事。基本上我有三個元素,2個TextViews和一個Imageview。我希望兩個文本框放在一起,右邊的圖像對齊右側,其視圖將它與圖像的大小相匹配。就像這樣:將佈局設置爲其內容的大小

Text 1      |==| 
Text 2 would be below text 1 |==| 

我的繼承人XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/entry_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:orientation="horizontal" 
    android:paddingRight="?android:attr/scrollbarSize" > 

    <LinearLayout 

     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/entry1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="left" 
      android:paddingLeft="@dimen/tab_host_default_height" 
      android:text="@string/test_string" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/entry2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="left" 
      android:paddingLeft="5dp" 
      android:paddingTop="5dp" 
      android:text="@string/test_string" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center" 
     > 

     <ImageButton 
      android:id="@+id/entry3" 
      style="@style/ImageButtonBDTheme" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/activated_background_holo_light" 
      android:contentDescription="@string/click_for_repair_report" 
      android:paddingRight="5dp" 
      android:src="@drawable/entry_link" /> 

    </LinearLayout> 

</LinearLayout> 

問題是,我不能要顯示的圖像沒有硬編碼包含兩個文本框的LinearLayout的大小。

任何幫助表示讚賞!

回答

1
// try this 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/entry_container" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="5dp" 
       android:minHeight="?android:attr/listPreferredItemHeight" 
       android:paddingRight="?android:attr/scrollbarSize" 
       android:gravity="center"> 

    <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="1" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

     <TextView 
       android:id="@+id/entry1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ellipsize="marquee" 
       android:fadingEdge="horizontal" 
       android:paddingLeft="@dimen/tab_host_default_height" 
       android:text="@string/test_string" 
       android:textStyle="bold" /> 

     <TextView 
       android:id="@+id/entry2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ellipsize="marquee" 
       android:fadingEdge="horizontal" 
       android:text="@string/test_string" /> 
    </LinearLayout> 

    <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:orientation="vertical"> 

     <ImageButton 
       android:id="@+id/entry3" 
       style="@style/ImageButtonBDTheme" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/activated_background_holo_light" 
       android:contentDescription="@string/click_for_repair_report" 
       android:paddingRight="5dp" 
       android:src="@drawable/entry_link" /> 

    </LinearLayout> 

</LinearLayout> 
+0

你明白了!感謝您的快速回復:) – xceph

+0

你可能可以解釋這是如何工作的?我會認爲在一箇中設置權重,但不是另一個只會顯示該元素。 – xceph

+0

什麼尺寸是需要顯示imagebutton它將採取和其餘的大小是由textview佈局使用.. –

1

試試這個..

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/entry_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:orientation="horizontal" 
    android:paddingRight="?android:attr/scrollbarSize" > 

    <LinearLayout 
     android:layout_width="0dp"     //changes here 
     android:layout_weight="0.6"     //changes here 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/entry1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="left" 
      android:paddingLeft="@dimen/tab_host_default_height" 
      android:text="@string/test_string" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/entry2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" 
      android:gravity="left" 
      android:paddingLeft="5dp" 
      android:paddingTop="5dp" 
      android:text="@string/test_string" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp"    //changes here 
     android:layout_weight="0.4"    //changes here 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:gravity="center" 
     > 

     <ImageButton 
      android:id="@+id/entry3" 
      style="@style/ImageButtonBDTheme" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/activated_background_holo_light" 
      android:contentDescription="@string/click_for_repair_report" 
      android:paddingRight="5dp" 
      android:src="@drawable/entry_link" /> 

    </LinearLayout> 

</LinearLayout> 
+0

謝謝,但設置重量不符合圖像的大小。我試圖讓佈局包含視圖的確切大小的圖像+填充,與LinearLayout包含文本使用其餘 – xceph

+0

@xceph然後給圖像佈局寬度作爲wrap_content和文本佈局權重爲1 – Hariharan

1

始終使用相對佈局,它需要較少沒有。的代碼相比,其他佈局。

請嘗試下面,我想這是你想要的,根據你的要求設置填充/保證金。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/entry_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:orientation="horizontal" 
    android:paddingRight="?android:attr/scrollbarSize" > 

    <TextView 
     android:id="@+id/entry1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" 
     android:gravity="left" 
     android:paddingLeft="@dimen/tab_host_default_height" 
     android:text="@string/test_string" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/entry2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/entry1" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" 
     android:gravity="left" 
     android:paddingLeft="5dp" 
     android:paddingTop="5dp" 
     android:text="@string/test_string" /> 

    <ImageButton 
     android:id="@+id/entry3" 
     style="@style/ImageButtonBDTheme" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/ic_launcher" 
     android:contentDescription="@string/click_for_repair_report" 
     android:paddingRight="5dp" 
     android:src="@drawable/entry_link" /> 

</RelativeLayout> 
+0

謝謝,這也工作,確實小得多。 – xceph

+0

在這種情況下,你可以upvote我的答案。 –

+0

你有它的傢伙 – xceph

相關問題