I hate to ask this question; for some reason I can't figure it out on my own. I have a vertical LinearLayout that contains a custom View that I've defined in my app and and a TextView. I'd like my custom View to be ali开发者_C百科gned on the top of the screen and the TextView to be aligned at the bottom of the screen. Unfortunately, no matter what changes I make to the attributes of the XML file, my custom View always seems to be centered vertically when I run the emulator. The xml is like the following:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="top">
<CustomView
android:id="@+id/customID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tileSize="24"
android:focusableInTouchMode="true" android:layout_gravity="top"/>
<TextView
android:id="@+id/text"
android:text="@string/droidtactoe_layout_text"
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="24sp" android:textColor="#00000000" android:layout_gravity="bottom"/>
</LinearLayout>
First, given your rules ("I'd like my custom View to be aligned on the top of the screen and the TextView to be aligned at the bottom of the screen"), I would recommend a RelativeLayout
, as it will be simpler. Just use android:layout_alignParentTop="true"
on the CustomView
and android:layout_alignParentBottom="true"
on the TextView
, and dump all the gravity
/layout_gravity
stuff.
If your CustomView
still misbehaves, temporarily replace it with a Button
or something. If the Button
then lays out as you expect, then there is something wrong in the implementation of your CustomView
. If you are inheriting from a real View
(e.g., CustomView
is a subclass of TextView
), you should inherit all the right functionality for laying things out. If your CustomView
is simply a subclass of View
, there might be some things you need to override or call that you aren't. I haven't made a custom View
subclass, only subclasses of stuff like LinearLayout
, so I'm not 100% certain what you would be missing.
Thank you! It was a problem in the CustomView. I was using a modified version of the overridden View from the Snake demo. That demo was using an X and Y offset that was defined in onSizeChanged based on the height and width of the View. I was then using those offsets in the onDraw method to pad the dimensions of the object I was trying to draw.
For a reason that doesn't make sense to me now, I assumed that the width and height of the view would be limited based on the content that was being written in onDraw b/c of setting the layout Width and Height to wrapContent. However, the width and height that was being passed in to onSizeChanged was the dimensions of the entire screen. As a result, those offsets were forcing my Custom View to fill the whole screen. I removed the offsets and the problem seems to be solved.
Again, thank you very much!
精彩评论