Android标签布局setContent

Android标签布局setContent,第1张

概述我正在开发一个带有标签布局的 Android应用程序.我已经把它带到了它不会产生像 Google’s tutorial suggested这样的新活动的地方,但是我只是为了在点击每个标签时显示我的内容.目前它只显示黑色,无论哪个标签处于活动状态. 以下是我最新迭代中的代码: Main.java public class Main extends TabActivity { /** Call 我正在开发一个带有标签布局的 Android应用程序.我已经把它带到了它不会产生像 Google’s tutorial suggested这样的新活动的地方,但是我只是为了在点击每个标签时显示我的内容.目前它只显示黑色,无论哪个标签处于活动状态.

以下是我最新迭代中的代码:

Main.java

public class Main extends tabactivity {    /** Called when the activity is first created. */    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        Resources res = getResources();        TabHost tabHost = getTabHost();        TabHost.TabSpec spec;        // add orders tab        spec = tabHost.newTabSpec("orders").setIndicator("Orders",res.getDrawable(R.drawable.flash_36))                          .setContent(R.ID.ordersLayout);        tabHost.addTab(spec);        // add positions tab        spec = tabHost.newTabSpec("positions").setIndicator("positions",res.getDrawable(R.drawable.small_tiles_36))                          .setContent(R.ID.positionsLayout);        tabHost.addTab(spec);        // add strategIEs tab        spec = tabHost.newTabSpec("strategIEs").setIndicator("StrategIEs",res.getDrawable(R.drawable.cards_36))                          .setContent(R.ID.strategIEsLayout);        tabHost.addTab(spec);        // add account tab        spec = tabHost.newTabSpec("account").setIndicator("Account",res.getDrawable(R.drawable.seal_36))                          .setContent(R.ID.accountLayout);        tabHost.addTab(spec);        tabHost.setCurrentTab(1);    }}

main.xml中

<?xml version="1.0" enCoding="utf-8"?><TabHost    androID:ID="@androID:ID/tabhost"             androID:layout_wIDth="fill_parent"             androID:layout_height="fill_parent"            xmlns:androID="http://schemas.androID.com/apk/res/androID">            <linearLayout   androID:ID="@+ID/mainLayout"                             androID:layout_wIDth="fill_parent"                             androID:layout_height="fill_parent"                            androID:padding="5dp">                <TabWidget  androID:ID="@androID:ID/tabs"                             androID:layout_wIDth="fill_parent"                             androID:layout_height="wrap_content">                </TabWidget>                <FrameLayout androID:ID="@androID:ID/tabcontent"                             androID:layout_wIDth="fill_parent"                             androID:layout_height="fill_parent"                            androID:background="#fff"                            androID:padding="5dp">                            <include layout="@layout/orders"/>                            <include layout="@layout/positions"/>                            <include layout="@layout/strategIEs"/>                            <include layout="@layout/account"/>                </FrameLayout>            </linearLayout></TabHost>

Account.xml在

<?xml version="1.0" enCoding="utf-8"?><linearLayout     androID:ID="@+ID/accountLayout"     androID:layout_wIDth="fill_parent"     androID:layout_height="fill_parent"     androID:background="#fff"    xmlns:androID="http://schemas.androID.com/apk/res/androID">    <TextVIEw         androID:text="Account information"         androID:ID="@+ID/accountLabel"         androID:layout_wIDth="fill_parent"         androID:layout_height="wrap_content">    </TextVIEw></linearLayout>

orders.xml中

我只包括这两个,它们与linearLayout的androID:ID参数的相应ID完全相同.

<?xml version="1.0" enCoding="utf-8"?><linearLayout     androID:ID="@+ID/ordersLayout"     androID:layout_wIDth="fill_parent"     androID:layout_height="fill_parent"     androID:background="#fff"    xmlns:androID="http://schemas.androID.com/apk/res/androID">    <TextVIEw         androID:text="Working Orders"         androID:ID="@+ID/ordersLabel"         androID:layout_wIDth="fill_parent"         androID:layout_height="wrap_content">    </TextVIEw></linearLayout>

这是我在标签之间切换时获得的屏幕截图:

订单标签

位置标签

这对于模拟器和我刚刚获得的三星galaxy都是如此,我强烈推荐的方式,任何想法?

解决方法 我想通了,它是main.xml文件的一个简单参数:

Main.xml(修订版)

<?xml version="1.0" enCoding="utf-8"?><TabHost    androID:ID="@androID:ID/tabhost"             androID:layout_wIDth="fill_parent"             androID:layout_height="fill_parent"            xmlns:androID="http://schemas.androID.com/apk/res/androID">            <linearLayout   androID:ID="@+ID/mainLayout"                             androID:layout_wIDth="fill_parent"                             androID:layout_height="fill_parent"                            androID:orIEntation="vertical"                            androID:padding="5dp">                <TabWidget  androID:ID="@androID:ID/tabs"                             androID:layout_wIDth="fill_parent"                             androID:layout_height="wrap_content">                </TabWidget>                <FrameLayout androID:ID="@androID:ID/tabcontent"                             androID:layout_wIDth="fill_parent"                             androID:layout_height="fill_parent"                            androID:background="#fff"                            androID:padding="5dp">                            <include layout="@layout/orders"/>                            <include layout="@layout/positions"/>                            <include layout="@layout/strategIEs"/>                            <include layout="@layout/account"/>                </FrameLayout>            </linearLayout></TabHost>

注意linearLayout,它现在包含参数androID:orIEntation.它之前做的是将屏幕上的所有其他内容推向右侧.现在它可以正常工作.

注意:明天在办公室,我可以测试是否仍允许我旋转手机并使其以横向方向正确显示.如果没有,那么我可能不得不为水平创建一个单独的xml文件,除非有人对此有所了解.

总结

以上是内存溢出为你收集整理的Android标签布局setContent全部内容,希望文章能够帮你解决Android标签布局setContent所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1135223.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存