开发者

Nested Android Views, static header view

To make my explanation shorter, I've made this "mockup" of what I'm trying to achieve. I'd like to create a reusable header, footer and a generic viewgroup that I can fill with whatever content is required. A ListActivity, GridView... etc.

Nested Android Views, static header view

I've tried a few different approaches thus far with out any luck whatsoever. My first attempt was to write three basic views. A RelativeLayout that would act as a container. I used to add the header (worked), wrote the GridView (worked) and when trying to attach the footer with include it would never anchor at the bottom of the screen no matter which gravity I was using.

I'm currently attempting to go quite simple to learn android's view system. So, my first baby step. Get a GridView working with images pulled from an Adapter. (Essentially the 'Hello GridViews' demo from the android site - done, works)

Nested Android Views, static header view

Next baby step. Try and add a static header above the GridView... catastrophe. I feel like I'm missing an important stepping stone to get this working, any hints in the right direction would be appreciated. I don't quite understand why the LinearLayout containing the Button is pushing the GridView off the screen (it's in the 开发者_开发问答HiearchyViewer) when the height is only "wrap_content" which should be "44dip".

Nested Android Views, static header view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <Button
            android:layout_width="fill_parent"
            android:layout_height="44dip"
            android:text="test"
        />
    </LinearLayout>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
         android:id="@+id/gridview"
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent"
         android:columnWidth="112dip"
         android:numColumns="auto_fit"
         android:verticalSpacing="0dp"
         android:horizontalSpacing="0dp"
         android:stretchMode="columnWidth"
         android:gravity="center"
    />
</LinearLayout>

// edit, fixed xml.

EDIT October 25, 2010: Here's the solution I'm using now. Each activity gets its own view which the activity inflates. The "reusable" views are included with

<?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"
>
<include layout="@layout/titlebar" />
<ListView
    android:id="@+id/standingsListView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:background="#FFF"
/>
<include layout="@layout/ad" />
</LinearLayout>


At least I can help you with the "Button" Problem: The magic attribute you missed is android:orientation

Without this attribute android take android:orientation="horizontal" as set. That means that every view is lined up on the horizontal line, so right of your button is your grid, which cant be displayed because your LinearLayout in which you have your button has layout_width="fill_parent"

One last tip: less is more, if you have only one view inside a layout, remove the layout and just place the view. (if you only want to have one button in your LinearLayout)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜