Android UI: what kind of layout options are mutually exclusive?
After designing layouts in Android for a while now, I still cannot get the hang of it. Often the results are unpredictable.
I think part of this boils down to some layout options not working well together. I.e. if you are using one way of specifying your layout, you're not meant to use some other way.
Notably, for me, changing layout_width
from fill_parent
to wrap_content
usually changes the world. Let me give you another specific example:
Part of my activity's layout is a LinearLayout:
<LinearLayout android:id="@+id/ll2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<LinearLayout android:id="@+id/ll2a" android:layout_width="fill_parent"
开发者_开发知识库 android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1">
<TextView android:id="@+id/l_cover" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/book_item_l_cover">
</TextView>
<ImageButton android:id="@+id/i_cover"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:scaleType="centerInside" android:gravity="center"
android:layout_weight="1" android:adjustViewBounds="true"
android:src="@drawable/spinner_black_76" android:minWidth="400dip">
</ImageButton>
</LinearLayout>
<LinearLayout android:id="@+id/ll2b" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical" android:layout_weight="1">
<TextView android:id="@+id/l_back_cover" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/book_item_l_back_cover">
</TextView>
<ImageButton android:id="@+id/i_back_cover"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:scaleType="centerInside" android:gravity="center"
android:layout_weight="1" android:adjustViewBounds="true"
android:src="@drawable/spinner_black_76">
</ImageButton>
</LinearLayout>
</LinearLayout>
To get 2 columns of left-aligning label-button combinations next to eachother I had to use the layout_weight
attributes. The ImageButtons shrink around their images due to the adjustViewBounds
attributes. This gets me a pretty good result.
Now what I finally also wanted was to have buttons of a certain minimum size so a finger can always easily press them, even when they do not contain any image (and they will shrink to only a few millimeters). I experimented with setting android:minWidth="400dip"
as you can see. I expected the button to fill up the entire half of the screen in width (400 is quite a large value), but I'm not seeing any difference.
I guess one of the other attributes above is preventing minWidth
from having any impact. And I could probably go experiment and disable some others to see what it gives me. But my question here is: does anybody have any (logical) information on which attribute prevents or counter-intuitively influences whichother? I'd like to get this sorted once and for all. Is this documented somewhere?
I've noticed that layout_weight seems to render minWidth useless.
Given three buttons in a linear layout, with the weight of the left and right to 0, and the center to 1, changing the minWidth of any of these has absolutely no effect on the way the view is rendered, until I remove the layout_weight attributes entirely for each button. I even made sure that the sum of all minWidths is less than the total view's width.
I have no idea why that is the case. The documentation regarding layout_weight:
This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view. Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight
I'd also really love to know what other layout parameters conflict with each other. It seems very unintuitive to me that defining a weight for a view to fill "remaining space in the parent view" would completely disregard the view's minWidth settings.
精彩评论