android布局xml属性和定位...

时间:2012-02-28 11:26:29

标签: android android-layout view properties

我遇到了问题,我尝试过google搜索和搜索其他方式,但我不知道该搜索什么...

我的布局是这样的:

 _______________________________________________________
|           |        |                                  |
|           |        |                                  |  
|  content  | button |                                  |
|           |        |                                  | 
|___________|________|__________________________________|                                                 

但如果我再添加一点,它将如下所示:

 _______________________________________________________
|                         |        |                    |
|                         |        |                    |  
|  content                | button |                    |
|                         |        |                    | 
|_________________________|________|____________________|                                                 

但是当内容比包装器大时,我想让内容可滚动但是在包装器的末尾显示按钮,如下所示:

 _______________________________________________________
|                                              |        |  
|                                              |        |  
|   scrollable content                         | button |  
|                                              |        |  
|______________________________________________|________|

所以不管有多少内容都没关系,按钮总是可见的,而且内容会被水平分解:)

我的代码现在是这样的,但我不知道该怎么做,所以我可以得到它,就像我想要的那样:

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="@dimen/TopBarHeight" >


        <HorizontalScrollView 
            android:layout_width="wrap_content"
            android:layout_height="@dimen/TopBarHeight" >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="@dimen/TopBarHeight" >
                <LinearLayout
                    android:layout_width="@dimen/TabMaxWidth"
                    android:layout_height="@dimen/TopBarHeight" />
                <LinearLayout
                    android:layout_width="@dimen/TabMaxWidth"
                    android:layout_height="@dimen/TopBarHeight" />
                <LinearLayout
                    android:layout_width="@dimen/TabMaxWidth"
                    android:layout_height="@dimen/TopBarHeight" />
                <LinearLayout
                    android:layout_width="@dimen/TabMaxWidth"
                    android:layout_height="@dimen/TopBarHeight" />
                <LinearLayout
                    android:layout_width="@dimen/TabMaxWidth"
                    android:layout_height="@dimen/TopBarHeight" />
                <LinearLayout
                    android:layout_width="@dimen/TabMaxWidth"
                    android:layout_height="@dimen/TopBarHeight" />
            </LinearLayout>             
        </HorizontalScrollView>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="@dimen/TopBarHeight" >
            <ImageButton 
                android:layout_width="@dimen/TopBarHeight"
                android:layout_height="@dimen/TopBarHeight"/>
        </LinearLayout>
    </LinearLayout>

3 个答案:

答案 0 :(得分:0)

我建议使用顶部的LinearLayout,总布局权重为4,水平。 然后为内容创建另一个线性布局,给它一个权重为3并在其中放置一个scrollview。创建另一个权重为1的linearlayout,并将该按钮添加到该布局。

或者使用relativelayout。然后,您可以指定scrollview对齐父级左侧和按钮右侧。按钮与滚动视图的父右侧和右侧对齐。

EDITED:这是一个可以做你想做的RelativeLayout的例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" android:layout_toLeftOf="@id/button1">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>
</ScrollView>
<Button
    android:id="@+id/button1"
    android:layout_width="100dp"
    android:layout_height="48dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button" />
</RelativeLayout>

答案 1 :(得分:0)

<?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">


    <HorizontalScrollView android:layout_height="@dimen/TopBarHeight" android:layout_width="wrap_content" android:layout_weight="1">
        <LinearLayout android:layout_width="wrap_content"
            android:layout_height="@dimen/TopBarHeight">
            <LinearLayout android:layout_width="@dimen/TabMaxWidth"
                android:layout_height="@dimen/TopBarHeight" />
            <LinearLayout android:layout_width="@dimen/TabMaxWidth"
                android:layout_height="@dimen/TopBarHeight" />
            <LinearLayout android:layout_width="@dimen/TabMaxWidth"
                android:layout_height="@dimen/TopBarHeight" />
            <LinearLayout android:layout_width="@dimen/TabMaxWidth"
                android:layout_height="@dimen/TopBarHeight" />
            <LinearLayout android:layout_width="@dimen/TabMaxWidth"
                android:layout_height="@dimen/TopBarHeight" />
            <LinearLayout android:layout_width="@dimen/TabMaxWidth"
                android:layout_height="@dimen/TopBarHeight" />
        </LinearLayout>
    </HorizontalScrollView>
    <ImageButton android:layout_width="@dimen/TopBarHeight" android:layout_height="@dimen/TopBarHeight" android:src="@drawable/icon"></ImageButton>
</LinearLayout>

答案 2 :(得分:0)

包含ImageButton的Horizo​​ntalScrollView和LinearLayout的用户布局重力如下所示。

变化如下点

  1. 在Horizo​​ntalScrollView中添加android:layout_weight =“1”

  2. 在Horizo​​ntalScrollView中添加android:layout_width =“fill_parent”

  3. 在包含Imagebutton的第二个Linearlayout中添加android:layout_weight =“4”

            <HorizontalScrollView 
                android:layout_width="fill_parent"
                android:layout_height="@dimen/TopBarHeight" 
    android:layout_weight="1">
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="@dimen/TopBarHeight" >
                    <LinearLayout
                        android:layout_width="@dimen/TabMaxWidth"
                        android:layout_height="@dimen/TopBarHeight" />
                    <LinearLayout
                        android:layout_width="@dimen/TabMaxWidth"
                        android:layout_height="@dimen/TopBarHeight" />
                    <LinearLayout
                        android:layout_width="@dimen/TabMaxWidth"
                        android:layout_height="@dimen/TopBarHeight" />
                    <LinearLayout
                        android:layout_width="@dimen/TabMaxWidth"
                        android:layout_height="@dimen/TopBarHeight" />
                    <LinearLayout
                        android:layout_width="@dimen/TabMaxWidth"
                        android:layout_height="@dimen/TopBarHeight" />
                    <LinearLayout
                        android:layout_width="@dimen/TabMaxWidth"
                        android:layout_height="@dimen/TopBarHeight" />
                </LinearLayout>             
            </HorizontalScrollView>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="@dimen/TopBarHeight" 
    android:layout_weight="4">
                <ImageButton 
                    android:layout_width="@dimen/TopBarHeight"
                    android:layout_height="@dimen/TopBarHeight"/>
            </LinearLayout>
        </LinearLayout>