在Android中实现布局作为背景

时间:2012-09-18 07:03:08

标签: android android-layout background

在我的应用程序中,我创建了一个包含5个按钮的相对布局,现在我想在其他布局中使用相同的布局作为背景。我正在尝试代码android:background+"@layout/dashboard",但是eclipse正在向我显示错误。我想将该布局显示为 Parent Bottom ,以便我可以实现滚动视图,并可以调用除信息中心之外的所有元素。请帮我告诉我:

  1. 如何将其他布局添加为背景
  2. 如何将该布局设置为alignParentBottom
  3. 如何修改scrollView以使仪表板保持静态,其余元素可以滚动..
  4. dashboard.xml的代码

    <?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="wrap_content" >
    
        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:text="Home" />
    
        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/b1"
            android:layout_alignParentBottom="true"
            android:text="Explore" />
    
        <Button
            android:id="@+id/b3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="MIC" />
    
        <Button
            android:id="@+id/b4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/b5"
            android:layout_alignParentBottom="true"
            android:text="Msngr" />
    
        <Button
            android:id="@+id/b5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:text="Profile" />
    
    </RelativeLayout>
    

    showdashboard.xml的代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@layout/dashboard" >
    
    </LinearLayout>
    

    我得到的错误是:

    无法解析文件C:\ Users \%username%\ Documents \ eclipse \ CustomList \ res \ layout \ dashboard.xml Window&gt;中记录了异常详细信息。显示视图&gt;错误记录

    请帮助..

2 个答案:

答案 0 :(得分:1)

由于您希望在页面底部有一个静态视图pf按钮,您可以将这些按钮包装成线性布局,然后将其余按钮放在滚动视图中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.7"
    android:id="@+id/s_view" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <!-- You can put your components here -->
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.3"
    android:orientation="horizontal" >

    <!-- your 5 buttons here -->
    <Button
        android:layout_width="match_parent"
        android:layout_height="20dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="20dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="20dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="20dp" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="20dp" />
</LinearLayout>

您可以通过调整布局权重来更改底部布局/滚动视图的大小。

如果您想为多个活动使用相同的布局,请制作单独的线性布局(如果您愿意,可以使用其他布局),然后您可以将它们添加到活动中的scrollView中。

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_layout);
    ScrollView sv = (ScrollView) findViewById(R.id.s_view);
    LayoutInflater lInflator = getLayoutInflater();
    sv.addView(lInflator.inflate(R.layout.view_component, null));

}

其中s_view是滚动视图的id,组件视图是目标布局。

答案 1 :(得分:0)

我想通过您的写作获得Static Buttons但其他布局应该有ScrollView

所以你可以通过这种布局来做到这一点

<?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" android:layout_weight="1.0" android:background="#ffffff" android:orientation="vertical">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:layout_weight="1.0" android:background="#ffffff" android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff"
    android:orientation="vertical" android:layout_weight="1.0">
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0">
            ---Your Scroll Vieww Layout hereee-------
        </ScrollView>
    </LinearLayout>
</LinearLayout>

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:background="#F3F0EC" android:orientation="horizontal">

        ----your  5 Button Layout hereee-------
</LinearLayout>
</LinearLayout>