Android的膨胀的FrameLayout里面tabcontent [英] Android inflate framelayout inside tabcontent

查看:251
本文介绍了Android的膨胀的FrameLayout里面tabcontent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助来解决简单的问题。
如何动态地连接,或夸大布局到标签的内容。
我想TabsHost项目。对于每一个标签,我喜欢不同的布局。很简单:)
我创建默认的Andr​​oid项目:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.tabtest
安卓版code =1
机器人:=的versionName1.0><用途-SDK
    安卓的minSdkVersion =14
    机器人:targetSdkVersion =18/><应用
    机器人:allowBackup =真
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:名字=com.example.tabtest.MainActivity
        机器人:标签=@字符串/ APP_NAME>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>

< /清单>

下一步我用eclipse设计师和拖放到TabHost activity_main。

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。>< TabHost
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真正的>    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:方向=垂直>        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT>
        < / TabWidget>        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent>            <的LinearLayout
                机器人:ID =@ + ID / TAB1
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent>
            < / LinearLayout中>            <的LinearLayout
                机器人:ID =@ + ID / TAB2
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent>
            < / LinearLayout中>            <的LinearLayout
                机器人:ID =@ + ID / TAB3
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent>
            < / LinearLayout中>
        < /&的FrameLayout GT;
    < / LinearLayout中>
< / TabHost>< / RelativeLayout的>

我增加了对TAB1称为类新的布局文件下一步

 <?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / FrameLayout1
机器人:layout_width =match_parent
机器人:layout_height =match_parent>< ListView控件
    机器人:ID =@ + ID / ListView1的
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT>
< /&的ListView GT;< /&的FrameLayout GT;

和类别Java类

所有的例子,我从互联网使用不要使用日食设计师TabHost定义,为什么?

我如何需要两个布局在code连接?

感谢。


解决方案

  //尝试这种方式,希望这将帮助你...tab1.xml
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:比重=中心>    <的TextView
        机器人:ID =@ + ID / txtTab1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=TAB1
        机器人:TEXTSIZE =25sp/>< / LinearLayout中>tab2.xml
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:比重=中心>   <的TextView
       机器人:ID =@ + ID / txtTab2
       机器人:layout_width =WRAP_CONTENT
       机器人:layout_height =WRAP_CONTENT
       机器人:文字=TAB2
       机器人:TEXTSIZE =25sp/>< / LinearLayout中>tab3.xml
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:比重=中心>    <的TextView
        机器人:ID =@ + ID / txtTab3
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=TAB3
        机器人:TEXTSIZE =25sp/>< / LinearLayout中>activity_main.xml中
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:上下文=MainActivity。>    < TabHost
        机器人:ID =@ + ID / mytabhost
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真正的>        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:方向=垂直>            < TabWidget
                机器人:ID =@机器人:ID /标签
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT>
            < / TabWidget>            <的FrameLayout
                机器人:ID =@机器人:ID / tabcontent
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent>                <包括
                    机器人:ID =@ + ID / TAB1
                    布局=@布局/ TAB1>
                < / - 包括GT;                <包括
                    机器人:ID =@ + ID / TAB2
                    布局=@布局/ TAB2>
                < / - 包括GT;
                <包括
                    机器人:ID =@ + ID / TAB3
                    布局=@布局/ TAB3>
                < / - 包括GT;            < /&的FrameLayout GT;
        < / LinearLayout中>
    < / TabHost>< / RelativeLayout的>MainActivity.java
公共类MainActivity延伸活动{    私人TabHost mytabhost;
    私人TextView的txtTab1;
    私人TextView的txtTab2;
    私人TextView的txtTab3;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        mytabhost =(TabHost)findViewById(R.id.mytabhost);
        txtTab1 =(的TextView)findViewById(R.id.txtTab1);
        txtTab2 =(的TextView)findViewById(R.id.txtTab2);
        txtTab3 =(的TextView)findViewById(R.id.txtTab3);
        mytabhost.setup();
        mytabhost.addTab(mytabhost.newTabSpec(\"Tab1\").setIndicator(\"MyTab1\",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
        mytabhost.addTab(mytabhost.newTabSpec(\"Tab2\").setIndicator(\"MyTab2\",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab2));
        mytabhost.addTab(mytabhost.newTabSpec(\"Tab3\").setIndicator(\"MyTab3\",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab3));        txtTab1.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(MainActivity.this,我的选项1,Toast.LENGTH_SHORT).show();
            }
        });
        txtTab2.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(MainActivity.this,我的选项2,Toast.LENGTH_SHORT).show();
            }
        });
        txtTab3.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(MainActivity.this,我的选项3,Toast.LENGTH_SHORT).show();
            }
        });
    }}

I need yours help to resolve simple issue . How dynamically "connect" or inflate layout into Tab content. I want project with TabsHost . For each tab I like different layouts. Very simple:) I am creating default android project :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tabtest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.tabtest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

< /manifest>

Next step I use eclipse designer and drag and drop TabHost into activity_main.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>

</RelativeLayout>

Next step I adding new layout file for tab1 called categories

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</FrameLayout>

and Categories java class

All examples I used from internet do not use eclipse designer for TabHost definition ,Why?

How do I need to connect two layouts in code ?

Thanks.

解决方案

// try this way,hope this will help you...

tab1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/txtTab1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tab1"
        android:textSize="25sp"/>

</LinearLayout>

tab2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

   <TextView
       android:id="@+id/txtTab2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Tab2"
       android:textSize="25sp"/>

</LinearLayout>

tab3.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/txtTab3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tab3"
        android:textSize="25sp"/>

</LinearLayout>

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TabHost
        android:id="@+id/mytabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <include
                    android:id="@+id/tab1"
                    layout="@layout/tab1" >
                </include>

                <include
                    android:id="@+id/tab2"
                    layout="@layout/tab2" >
                </include>
                <include
                    android:id="@+id/tab3"
                    layout="@layout/tab3" >
                </include>

            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

MainActivity.java
public class MainActivity extends Activity {

    private TabHost mytabhost;
    private TextView txtTab1;
    private TextView txtTab2;
    private TextView txtTab3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mytabhost =(TabHost) findViewById(R.id.mytabhost);
        txtTab1 =(TextView) findViewById(R.id.txtTab1);
        txtTab2 =(TextView) findViewById(R.id.txtTab2);
        txtTab3 =(TextView) findViewById(R.id.txtTab3);
        mytabhost.setup();
        mytabhost.addTab(mytabhost.newTabSpec("Tab1").setIndicator("MyTab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
        mytabhost.addTab(mytabhost.newTabSpec("Tab2").setIndicator("MyTab2",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab2));
        mytabhost.addTab(mytabhost.newTabSpec("Tab3").setIndicator("MyTab3",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab3));

        txtTab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"My Tab 1",Toast.LENGTH_SHORT).show();
            }
        });
        txtTab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"My Tab 2",Toast.LENGTH_SHORT).show();
            }
        });
        txtTab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"My Tab 3",Toast.LENGTH_SHORT).show();
            }
        });
    }

}

这篇关于Android的膨胀的FrameLayout里面tabcontent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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