如何创建的所有活动的通用的Andr​​oid XML布局 [英] How to create a generic Android XML layout for all activities

查看:122
本文介绍了如何创建的所有活动的通用的Andr​​oid XML布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要同样的物品[5按钮作为选项卡]在每个屏幕的应用程序。是否有可能建立一个基地XML布局,有这5个按键,然后让所有其他的XML文件从某种方式浅布局延伸,使得我不必有这将最终具有相同的功能多个按钮。

I have an application that needs the same items [5 buttons acting as tabs] in every screen. Is it possible to create a "base XML layout" that has these 5 buttons and then have all the other XML files extend from the bas layout in some way so that I don't have to have multiple buttons that will ultimately have the same functionality.

有没有更好的办法处理这一问题,可以通过API 9支持

Is there a better approach to this problem that can be supported by API 9

推荐答案

创建你的基地活动的一个共同的布局。然后包括使用中的所有布局布局<包括> tagwhich你想在同一

Create a common layout for your base activity. and then include that layout in all the layout using the <include> tagwhich you want to make the same.

在这之后创建一个抽象的活动,然后处理的按钮和code本次活动的所有点击,然后延伸中,你必须包括基本布局中的所有其他活动本次活动。

After that create one Abstract Activity and then handle all the click of the buttons and code in this activity and then extends this activity in all other activity in which you have include the base layout.

例如

普通按钮XML布局

<?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="wrap_content"
    android:background="@drawable/tabhost_bg"
    android:gravity="center"
    android:orientation="horizontal" 
    android:weightSum="3">

    <Button
        android:id="@+id/btnHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_btn_active" 
        android:layout_weight="1"
        android:text="@string/label_home"
        style="@style/bottom_tab_btn"/>

    <Button
        android:id="@+id/btnSetting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_btn_active" 
        android:layout_weight="1"
        android:text="@string/label_settings"
        style="@style/bottom_tab_btn"/>

    <Button
        android:id="@+id/btnMore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_btn_active" 
        android:layout_weight="1"
        android:text="@string/label_more"
        style="@style/bottom_tab_btn"/>

</LinearLayout>

下面是一个XML布局,其中包括上面的XML文件

Here is a xml layout in which you can include above XML file

<include
        android:id="@+id/bottombar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/bottom_bar" />

下面的android:layout_width和android:layout_height和布局是强制性的属性。

Here android:layout_width and android:layout_height and layout are compulsory attributes

现在这里有一个基本活动负责处理共同控制点击

Now here is a Base Activity which handles the click of the common controls

public abstract class BottomBar extends Activity implements OnClickListener {

    protected Button btnHome;
    Button btnSetting, btnMore;
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
    }

    protected void mappingWidgets() {

        btnHome = (Button) findViewById(R.id.btnHome);
        btnSetting = (Button) findViewById(R.id.btnSetting);
        btnMore = (Button) findViewById(R.id.btnMore);

        btnHome.setOnClickListener(this);
        btnSetting.setOnClickListener(this);
        btnMore.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == null)
            throw new NullPointerException(
                    "You are refering null object. "
                            + "Please check weather you had called super class method mappingWidgets() or not");
        if (v == btnHome) {

        } else if (v == btnSetting) {

        }else if(v == btnMore) {

        }
    }

    protected void handleBackgrounds(View v) {
        if (v == btnHome) {
            btnHome.setBackgroundResource(R.drawable.bottom_btn_hover);
            btnSetting.setBackgroundResource(R.drawable.bottom_btn_active);
            btnMore.setBackgroundResource(R.drawable.bottom_btn_active);

        } else if (v == btnSetting) {
            btnHome.setBackgroundResource(R.drawable.bottom_btn_active);
            btnSetting.setBackgroundResource(R.drawable.bottom_btn_hover);
            btnMore.setBackgroundResource(R.drawable.bottom_btn_active);

        } else if (v == btnMore) {
            btnHome.setBackgroundResource(R.drawable.bottom_btn_active);
            btnSetting.setBackgroundResource(R.drawable.bottom_btn_active);
            btnMore.setBackgroundResource(R.drawable.bottom_btn_hover);
        }
    }

}

现在一步到位的剩余扩展了所有的活动这一基地的活动。

Now one step is remaining to extends this Base activity in all your activities.

您可以使用extends关键字扩展了活动的基本活动。 例如

You can extends the Base activity in activity using the extends keyword. For example

public class MyActivity extends BottomBar

注意:从孩子的活动,你必须要调用基类的父类方法来处理你的基地布局的共同控制点击

Note: From the child activity you must have to call the super method of the base class to handle the click of the common controls of your base layout.

您可以因此在您的单一活动实现多种常见的布局。

You can thus implement multiple common layout within your single activity.

希望这会帮助你。 享受!!

Hope this will help you. Enjoy!!

这篇关于如何创建的所有活动的通用的Andr​​oid XML布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆