2013-04-15 76 views
0

我有一个头,这是一个线性布局,并且具有一个文本视图和图像视图的右边。保持图像视图固定到线性布局机器人

< LinearLayout 
<TextView 
    android:layout_weight="8" 
    ... 
/> 
<ImageView 
    android:layout_weight="2" 
... 
/> 
</LinearLayout> 

在纵向模式下,一切都是完美的,但在横向模式下,由于头部的大小增加时,ImageView的移动有点向左。

我希望能够将图像视图固定在头部的右侧,不管是定向的。

有没有办法做到这一点?

+2

你可能想考虑使用RelativeLayout。如果你这样做,你可以使用android:alignParentRight =“true” – atreat

+0

如果你的根布局是相对的,你可以设置textview'android:alignParentLeft'为true,并且通过放置imageview的id来设置'android:layout_toLeftOf'。当然,设置ImageView的'安卓PARAMS:alignParentRight'上真正的 – deadfish

回答

0

刚刚成立的ImageView layout_width和layout_height为 “WRAP_CONTENT”,并删除layout_weight。这样,TextView将填充ImageView左侧的所有可用空间。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 
    <TextView 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
    /> 
    <ImageView 
     android:src="@drawables/my_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    /> 
</LinearLayout> 
0

使用此

< LinearLayout 
...> 
<TextView 
    android:layout_weight="8" 
    ... 
/> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    > 
    <ImageView 
     android:alignParentRight="true" 
     ... 
    />  
</RelativeLayout> 

</LinearLayout> 
0

添加下面的代码,它会分配所有可用空间的texView的ImageView的转移到布局的右侧:

< LinearLayout 
<TextView 
android:layout_weight="1" 
... 
/> 
<ImageView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
/> 
</LinearLayout>