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

11

我有一个应用程序,每个屏幕都需要相同的项目 [5个按钮作为选项卡]。是否可以创建一个“基本 XML 布局”,其中包含这些 5 个按钮,并使所有其他 XML 文件以某种方式从基础布局扩展,以便我不必拥有最终具有相同功能的多个按钮。

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

3个回答

17

创建一个通用的布局作为基本活动,然后使用<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>

您可以使用以下 XML 布局包含上面的 XML 文件:

<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和layout是必填属性。

现在有一个基础Activity,它处理常见控件的点击事件。

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

}

现在仅剩的一步是在所有活动中扩展这个基础活动。

您可以使用extends关键字在活动中扩展基础活动。 例如:

public class MyActivity extends BottomBar

注意: 从子活动中,您必须调用基类的super方法来处理基本布局的公共控件的单击。

因此,您可以在单个活动中实现多个常见布局。

希望这能帮到您。 享受吧!


有没有一种方法可以在实现基本布局的布局中访问基本布局的ID? - zabawaba99
我认为没有必要在主布局中访问包含布局的组件。如果您真的想要访问,那么据我所知您可以使用它。 - Dharmendra
非常有用的帖子!! :) - Nitesh

3

您可能需要了解<include>标记。 它可以有效地将您创建的xml复制并粘贴到其他布局中。

因此,您需要创建一个具有按钮的单个布局。将它们放在<merge>标记之间,以便它们不会创建FrameLayout。然后使用<include>标记在其他布局中使用相同的布局。

注意:始终覆盖<include>标记中的layout_widthlayout_height属性。即使您在相同的值上进行覆盖,这也是正确的。在之前的Android版本中存在漏洞,如果您没有覆盖这些属性,则会忽略某些属性。


1

使用include可能是一个好的选择,但我自己从来没有成功地让它可靠地工作过。也许我做错了什么,但编译器并不总是能够在合并的布局中找到id。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接