如何为所有活动创建通用的Android XML布局

zab*_*a99 9 android android-layout

我有一个应用程序需要在每个屏幕中使用相同的项目[5个按钮充当选项卡].是否有可能创建一个具有这5个按钮的"基本XML布局",然后以某种方式从bas布局扩展所有其他XML文件,这样我就不必拥有最终具有相同功能的多个按钮.

是否有更好的方法来解决API 9可以支持的这个问题

Dha*_*dra 14

为基本活动创建公共布局.然后使用<include>您想要使用的标记在所有布局中包含该布局.

之后,创建一个抽象活动,然后处理此活动中按钮和代码的所有单击,然后在包含基本布局的所有其他活动中扩展此活动.

例如

常用按钮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>
Run Code Online (Sandbox Code Playgroud)

这是一个xml布局,您可以在其中包含上面的XML文件

<include
        android:id="@+id/bottombar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/bottom_bar" />
Run Code Online (Sandbox Code Playgroud)

这里android:layout_width和android:layout_height和layout是必需属性

现在这里是一个基本活动,它处理公共控件的点击

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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

现在剩下的步骤是在所有活动中扩展此Base活动.

您可以使用extends关键字扩展活动中的Base活动.例如

public class MyActivity extends BottomBar
Run Code Online (Sandbox Code Playgroud)

注意:从子活动中,您必须调用基类的超级方法来处理基本布局的常用控件的单击.

因此,您可以在单个活动中实现多个通用布局.

希望这会帮助你.请享用!!