【问题标题】:How to implement master and child page like Activity in Android?如何在Android中实现像Activity这样的主页面和子页面?
【发布时间】:2010-01-28 08:58:49
【问题描述】:

我也想在每个带有页脚的 Activity 上放置一个通用横幅和菜单。

谁能指导我如何在Android中实现像asp.net这样的主页面和子页面???

任何帮助将不胜感激。

【问题讨论】:

  • 有一个关于如何在 Android 中制作“母版页”布局的简短教程 - here

标签: android android-activity


【解决方案1】:

您可以让您的每个活动都扩展一个公共基类,该基类有一个 onCreateOptionsMenu 方法,每次都从相同的 XML 扩展菜单。虽然你不能有多重继承,但是当你想要简单的活动和列表活动时,这可能会很棘手。

另一种方法是创建一个 Util 类,其中您有一个类似 setupMenu(Menu) 的方法,如果您正在执行一些更复杂的菜单设置,您的每个活动都可以调用该方法。

就每个活动的 XML UI 布局而言,您可以使用 <include/> 标签包含一个通用横幅。

【讨论】:

  • 我的答案中的链接有一个例子。 Android 主屏幕应用程序使用 标记多次包含整个workspace_screen.xml 布局。
【解决方案2】:

解决方案非常简单。

您需要在 onCreate 函数 SetContentView 中扩展“Activity”类到您的基本 xml 布局,还需要覆盖基本 Activity 类中的 setContentView

例如:

1.使用以下代码创建“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="fill_parent"
      android:orientation="vertical"
      android:background="#000000"
      android:padding="15dp" >
    <LinearLayout android:orientation="horizontal" android:background="#000000"
           android:layout_width="fill_parent" android:layout_height="wrap_content"
           android:minHeight="50dp" android:paddingLeft="10dp">
           <ImageView android:layout_width="wrap_content" android:id="@+id/ImageView01"
               android:adjustViewBounds="true" android:layout_height="wrap_content"
               android:scaleType="fitCenter" android:maxHeight="50dp" />
   </LinearLayout>
   <LinearLayout android:id="@+id/linBase"
     android:layout_width="fill_parent"
       android:layout_height="fill_parent" >
   </LinearLayout>
</LinearLayout>    

2.创建“BaseActivity.java”

public class BaseActivity extends Activity {
    ImageView image;
    LinearLayout linBase;     
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        super.setContentView(R.layout.base_layout);        
        linBase = (LinearLayout)findViewById(R.id.linBase);
    }
    @Override
    public void setContentView(int id) {
        LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(id, linBase);
    }
}

public class SomeActivity extends BaseActivity {    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.some_layout);
       //rest of code
    }
}

到目前为止,我唯一注意到的是,在请求进度条时 (requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)),这需要在调用 super.onCreate 之前完成。我认为这是因为在调用此函数之前还不能绘制任何内容。

这对我来说非常有用,希望你会发现它在你自己的编码中很有用。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题并使用ActivityGroup 解决了它。 我想菜单项会将用户移动到另一个活动,因此在每个活动中使用相同的菜单关闭应用程序并使用 BACK 按钮几乎是不可能的(一段时间后,用户将不得不返回他曾经见过的所有活动)。

    我没有找到任何好的英文教程,所以前段时间写过我的(有点太短了,只有波兰语,但谷歌翻译的版本应该可以理解)检查this

    您也可以查看TabHost works的方式

    【讨论】:

      【解决方案4】:

      ViewStub 是解决方案

      activity_masterpage.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >    
      <ViewStub android:id="@+id/stub_content"
                     android:inflatedId="@+id/subTree"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent" />
      

      stub = (ViewStub) findViewById(R.id.stub_content);
      stub.setLayoutResource(R.layout.content_layout);
      stub.inflate(); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-15
        相关资源
        最近更新 更多