2013-10-09 30 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

相关问题