2012-11-04 40 views
0

我有一个可调整大小的应用部件。我想在图片的上方和下方有文字。图像应该占用尽可能多的空间,文字应该直接放在图像的上方和下方。可变父级大小的应用部件xml布局

我只想在xml中做到这一点,但不能找出适用于所附图像中显示的两种情况的布局。

所以我的问题是,用什么XML可以实现这种行为?

编辑:图像视图可能比父容器视图大。

desired layout behavior

+0

使用RelativeLayout和首先定义的图像。在父级中使用中心对齐方式。然后是两个textview,一个使用layoutAbove,另一个使用layoutBelow。 – Simon

回答

1

事情是这样的:

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

    <ImageView 
     android:id="@+id/imageview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scaleType="center" 
     android:layout_alignParentCenter="true"/> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/imageview" 
     android:text="text 1"/> 


    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/imageview" 
     android:text="text 2"/> 


</RelativeLayout> 

还是这个,使用布局重来控制大小:

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_weight="10" 
     android:layout_height="0px" 
     android:text="text 1"/> 

    <ImageView 
     android:id="@+id/imageview" 
     android:layout_width="wrap_content" 
     android:layout_weight="80" 
     android:layout_height="0px" 
     android:scaleType="center" 
     android:layout_alignParentCenter="true"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="match_parent" 
     android:layout_weight="10" 
     android:layout_height="0px" 
     android:text="text 2"/> 

</LinearLayout> 

播放使用权,影像对准和图像比例键入以获得它你想要的。

+0

感谢您的建议,但我已经尝试过这种布局,并且它不适用于场景1.图像将填充所有可用高度,而不显示文本视图。 (顺便说一句,我还必须添加adjustviewbounds = true,使您的场景2中的建议布局工作) – timothyjc

+0

我编辑了另一个可能的解决方案。我还编辑了Re​​lativeLayout以包含scaleType。 – Simon

+0

再次感谢,但使用权重对情景1不起作用。蓝色文字视图要么被切断,要么占用更多空间,如果他们只是包装其内容...... – timothyjc