安卓:启动意图进入的FrameLayout意图、FrameLayout

由网友(你给的伤数不清)分享简介:我有这个布局文件主要活动:< XML版本=1.0编码=UTF-8&GT?;<的LinearLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:layout_height =FILL_PARENT机器人:layout_width...

我有这个布局文件主要活动:

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_height =FILL_PARENT
  机器人:layout_width =FILL_PARENT
  机器人:方向=垂直
  机器人:ID =@ + ID /容器
  >
    <的LinearLayout
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID /头
        机器人:背景=@可绘制/ logo_bk
        机器人:layout_width =FILL_PARENT
    >

    <按钮
        机器人:ID =@ + ID / btn_reload
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=刷新
     />

     <的LinearLayout
        机器人:ID =@ + ID / LinearLayout01
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:重力=center_vertical | center_horizo​​ntal
     >
        < ImageView的
            机器人:ID =@ + ID / ImageView01
            机器人:SRC =@可绘制/ logo_head
            机器人:scaleType =fitStart
            机器人:adjustViewBounds =真
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT/>
     < / LinearLayout中>

    < / LinearLayout中>

    <的FrameLayout
        机器人:ID =@ + ID /中心
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =FILL_PARENT
        机器人:layout_weight =1>
    < /的FrameLayout>

    <的LinearLayout
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =FILL_PARENT
        机器人:重力=中心
        机器人:ID =@ + ID /页脚
        机器人:layout_weight =2.6
        机器人:后台=#FFFFFF>
    < / LinearLayout中>

< / LinearLayout中>
 

基本上,它是由一个头,一个中心部分(机器人:ID =@ + ID /中心)和页脚。 页脚包含四个动态创建的按钮。在最后它看起来像标签在底部TabWidget。

每个页脚的按钮持有意图/活动。

现在的问题是:我如何开始我的活动进入的FrameLayout? 例如TabHost做到这一点:

 规范= tabHost
        .newTabSpec(tabTitle.toLowerCase())
        .setIndicator(tabTitle,res.getDrawable(R.drawable.tab_spec))
        .setContent(意向);
        tabHost.addTab(规范);
 
基于FrameLayout的onLayout分析

解决方案

如果您有您想要显示的每个页面独立的活动,你将不得不延长的ActivityGroup的容器活动(一个显示的选项卡)和LocalActivityManager管理要使用不同的嵌入式的活动。

这,有点复杂,没有记录。我不得不读取源$ C ​​$ C的 TabHost 。

搜索类IntentContentStrategy

基本上,这个想法是,你有一个容器视图,并使用LocalActivityManager加载活动,得到它的观点,并将其放置在容器视图中。

从TabHost.java摘录:

 公开查看getContentView(){
        如果(mLocalActivityManager == NULL){
            抛出新IllegalStateException异常(你忘了叫'公共无效设置(LocalActivityManager的ActivityGroup)'?);
        }
        最后一个窗口W = mLocalActivityManager.startActivity(
                MTAG,mIntent);
        最后查看WD = W!= NULL? w.getDecorView():空;
        如果(mLaunchedView = WD&放大器;!&安培;!mLaunchedView = NULL){
            如果(mLaunchedView.getParent()!= NULL){
                mTabContent.removeView(mLaunchedView);
            }
        }
        mLaunchedView = WD;

        // XXX集FOCUS_AFTER_DESCENDANTS嵌入式活动,所以现在他们可以得到
        //重点,如果没有自己的孩子拥有它。他们不必集中,以便能够
        //显示的菜单项。
        //
        //东西时错误628886固定好替换此...
        //
        如果(mLaunchedView!= NULL){
            mLaunchedView.setVisibility(View.VISIBLE);
            mLaunchedView.setFocusableInTouchMode(真正的);
            ((ViewGroup中)mLaunchedView).setDescendantFocusability(
                    FOCUS_AFTER_DESCENDANTS);
        }
        返回mLaunchedView;
    }
 

注:有很多的调整和怪异的东西,你必须做的就是这个东西的工作(注意他们的意见有引用错误),这可能是为什么它不记录在案的。

I have a main activity with this layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:orientation="vertical"
  android:id="@+id/container"
  >
    <LinearLayout
        android:layout_height="wrap_content"
        android:id="@+id/header" 
        android:background="@drawable/logo_bk"
        android:layout_width="fill_parent"
    >

    <Button 
        android:id="@+id/btn_reload"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Reload" 
     />

     <LinearLayout 
        android:id="@+id/LinearLayout01" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:gravity="center_vertical|center_horizontal"
     >
        <ImageView 
            android:id="@+id/ImageView01" 
            android:src="@drawable/logo_head" 
            android:scaleType="fitStart" 
            android:adjustViewBounds="true" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" />
     </LinearLayout>

    </LinearLayout>

    <FrameLayout 
        android:id="@+id/center" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:layout_weight="1">
    </FrameLayout>

    <LinearLayout 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent" 
        android:gravity="center" 
        android:id="@+id/footer"
        android:layout_weight="2.6"
        android:background="#ffffff">
    </LinearLayout>

</LinearLayout>

Basically it is composed of a header, a central part (android:id="@+id/center") and a footer. The footer contains four dynamically created buttons. At the end it looks like a TabWidget with the tabs at the bottom.

Each footer's buttons holds an intent/activity.

The question is: How can I start my activity into the FrameLayout? For instance TabHost does this:

spec = tabHost
        .newTabSpec(tabTitle.toLowerCase())
        .setIndicator(tabTitle,res.getDrawable(R.drawable.tab_spec))
        .setContent(intent);
        tabHost.addTab(spec);

解决方案

If you have separate activities for each page you want to display, you're going to have to extend ActivityGroup for the container activity (the one displaying the tabs), and LocalActivityManager to manage the different embedded activities you want to use.

It's kind of complicated, and not documented. I had to read the sourcecode for TabHost.

Search for class IntentContentStrategy.

Basically, the idea is that you have a container view, and you use LocalActivityManager to load the activity, get it's View, and place it inside the container view.

Excerpt from TabHost.java:

    public View getContentView() {
        if (mLocalActivityManager == null) {
            throw new IllegalStateException("Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?");
        }
        final Window w = mLocalActivityManager.startActivity(
                mTag, mIntent);
        final View wd = w != null ? w.getDecorView() : null;
        if (mLaunchedView != wd && mLaunchedView != null) {
            if (mLaunchedView.getParent() != null) {
                mTabContent.removeView(mLaunchedView);
            }
        }
        mLaunchedView = wd;

        // XXX Set FOCUS_AFTER_DESCENDANTS on embedded activities for now so they can get
        // focus if none of their children have it. They need focus to be able to
        // display menu items.
        //
        // Replace this with something better when Bug 628886 is fixed...
        //
        if (mLaunchedView != null) {
            mLaunchedView.setVisibility(View.VISIBLE);
            mLaunchedView.setFocusableInTouchMode(true);
            ((ViewGroup) mLaunchedView).setDescendantFocusability(
                    FOCUS_AFTER_DESCENDANTS);
        }
        return mLaunchedView;
    }

Note: there is a lot of tweaking and weird stuff you have to do to get this stuff to work (notice the bug they reference in the comment there), which is probably why it's not documented.

阅读全文

相关推荐

最新文章