开发者

Android ImageViews not working correctly when weighted

This piece of XML will correctly display a button which covers the entire row then 2 buttons which are weigh开发者_StackOverflow社区ted evenly and share 50% of the space like so:

__________________________
|________________________|
||       ButtonA        ||
||______________________||
|____________ ___________|
|| ButtonB  | | ButtonC ||
||__________| |_________||
|________________________|

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_height="fill_parent" android:layout_width="fill_parent">
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:layout_weight="1">
    <Button android:id="@+id/b_a"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button A" />
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="fill_parent"
 android:layout_weight="1">
    <Button android:id="@+id/b_b"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button B" />
    <Button android:id="@+id/b_c"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Button C" />
</TableRow>

I want to replace Button A with an ImageView which will stretch across just like Button A is currently. However, when you replace ButtonA with:

<ImageView android:id="@+id/img_a"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src=@drawable/imagename />

This affects the weighting of Buttons B and C, making button B cover most of the screen and making C really squashed up. It appears to be related to the dimensions of the image (I used 320x50).

__________________________
|________________________|
||       Image A        ||
||______________________||
|___________________ ____|
|| ButtonB          ||bC||
||__________________||__||
|________________________|

How can I insert this ImageView without affecting the rest of the table rows?


I think you are being a little too liberal with your use of layout_weight. this is more an option used for having multiple items within a layout that need to be sized dynamically according to screen size (just like you have with buttons B and C). I would get rid of the rest of your layout_weights as they essentially arent doing anything and may have some underlying funky behavior. Also, layout_weight is a ratio of item-size to screen that ranges from 0-1. Both buttons B and C want to have 100% of the horizontal space allocated for them. Thus, I believe your issue is that button B is being prioritized higher than button C. try changing your layout_weight's for those 2 items to 0.5, This way they each desire 50% of the space allocated. Let me know how this works for you!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜