As, the header says, I'm working on an Android application with a Layout View and two rows of buttons for accomplishing various tasks. One row of buttons is across the top and one is across the bottom, and I like the way that looks.
Now for the unfortunate part: I used a pair of tools you're just not supposed use any more to accomplish this: an AbsoluteLayout, and button locations (and sizes) in px rather than dp or sp.
The thing is, even if I had used more advanced layout tools, I have no idea how I would've placed the set of buttons at the bottom of the screen. And that's for just one screen size, much less a 2nd one (which is what I've got now...)
Now, this isn't ever going to be a publicly-available application, so I'm not terribly worried about universal compatibility, but does anybody have suggestions for a simple layout XML description of one row of 5 buttons across both the top and bottom of the screen (which is fixed in landscape mode)???
Additionally, does anybody know if its possible to locate layout widgets at run time? It's very easy to re-size them, but not to change their locations, as far as I can tell?
T开发者_JAVA技巧hanks,
R.<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top">
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_weight="1"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="bottom" android:layout_gravity="bottom" android:layout_weight="1">
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="bottom"></Button>
<Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:text="Button" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
<Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_weight="1"></Button>
</LinearLayout>
</LinearLayout>
There's really no good short answer for your question. Your best bet would be to read of a tutorial that explains and shows you what's going on. I thought that this one was a great starting point for me.
There's a few ways you could accomplish what you're talking about but I'd probably use a RelativeLayout. There's a good explanation of how you can use them on Romain Guy's blog. In short, one set of buttons would each have layout_alignParentTop="true"
set, and the others would have layout_alignParentBottom
set to true - problem solved. You could also set android:id
s on the buttons so you can position them to the left/right of each other with android:layout_toLeftOf
/android:layout_toRightOf
, or you can stick each set of buttons in a LinearLayout
with android:orientation="horizontal"
.
As far as repositioning elements at runtime, you can definitely do that - but you have to do it via the element's LayoutParams
(either creating a new LayoutParams
object of the appropriate type and assigning it to the View
, or by calling getLayoutParams()
and modifying the returned object). For children of a RelativeLayout, see RelativeLayout.LayoutParams
.
Yes, this is easy to do using FrameLayout and layout_gravity.
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
...
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
...
</LinearLayout>
</FrameLayout>
Sandwiching a view in between the two button bars may require changing the FrameLayout to LinearLayout and setting the layout_weight on the center view to 1 while not specifying the weight of the button bars.
精彩评论