草庐IT

android - 两个 XML 文件,但其中一个在运行时未显示在应用程序中

coder 2024-07-02 原文

我在 res>layout forlder 中创建了一个名为 tabs.xml 的新 xml 文件。它有以下代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <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" >

                <RelativeLayout
                    android:id="@+id/Network"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true"
                        android:text="Channel Type:"
                        android:textAppearance="?android:attr/textAppearanceMedium" />

                    <RadioButton
                        android:id="@+id/urbanButton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_below="@+id/textView1"
                        android:text="Urban" />

                     <RadioButton
                         android:id="@+id/suburbanButton2"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_alignBaseline="@+id/urbanButton1"
                         android:layout_alignBottom="@+id/urbanButton1"
                         android:layout_centerHorizontal="true"
                         android:text="SubUrban" />

                     <RadioButton
                         android:id="@+id/ruralButton3"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_alignBaseline="@+id/suburbanButton2"
                         android:layout_alignBottom="@+id/suburbanButton2"
                         android:layout_alignParentRight="true"
                         android:layout_marginRight="15dp"
                         android:text="Rural" />

                     <TextView
                         android:id="@+id/textView2"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_below="@+id/urbanButton1"
                         android:text="Characteristics of Channel:"
                         android:textAppearance="?android:attr/textAppearanceMedium" />

                     <TextView
                         android:id="@+id/textView3"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_alignParentLeft="true"
                         android:layout_alignRight="@+id/suburbanButton2"
                         android:layout_below="@+id/textView2"
                         android:text="Medium Text"
                         android:textAppearance="?android:attr/textAppearanceMedium" />

                     <CheckBox
                         android:id="@+id/checkBox1"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_below="@+id/textView3"
                         android:layout_toLeftOf="@+id/ruralButton3"
                         android:text="Manual Override" />
                 </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/power"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/Connectivity"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/Offline"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </RelativeLayout>
            </FrameLayout>
        </LinearLayout>`enter code here`
    </TabHost>
</LinearLayout>

现在我在同一个文件夹中也有一个 andoid activity_main.xml,我没有向其中添加任何内容。我还创建了一个名为 Tabs.Java 的 java 文件,并将其放在 src>com.example.qosmetre>Tabs.Java 下的 src 文件夹中,代码如下

package com.example.qosmetre;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Tabs extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);
    }

}

现在我的问题是,当我在终端或 Debug模式下运行我的应用程序时,为什么除了空白屏幕什么也没有显示?就像我希望选项卡首先显示它们下面的所有内容,如代码所示。另外,我将如何导入用于我的应用程序中的散点图数据的库?

我需要知道将什么代码放在什么地方以及什么文件中,因为我只是一个初学者,我知道的不多,所以我需要大量的指导。

再次感谢。

我的android Manifest.xml 有以下代码:

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

  <uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="17" />

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.qosmetre.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>
  <activity 
        android:name=".MyTabsActivity" 
        </activity> 
  </application>`enter code here`

</manifest>

最佳答案

详情请引用TabActivity Android API .

弃用:

TabActivity is deprecated since API level 13.

For apps developing against HONEYCOMB or later, tabs are typically presented in the UI using the new ActionBar.newTab() and related APIs for placing tabs within their action bar area.

截图:

tabs.xml :

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <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" >

            <RelativeLayout
                android:id="@+id/Network"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="Channel Type:"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <RadioButton
                    android:id="@+id/urbanButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/textView1"
                    android:text="Urban" />

                <RadioButton
                    android:id="@+id/suburbanButton2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/urbanButton1"
                    android:layout_alignBottom="@+id/urbanButton1"
                    android:layout_centerHorizontal="true"
                    android:text="SubUrban" />

                <RadioButton
                    android:id="@+id/ruralButton3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/suburbanButton2"
                    android:layout_alignBottom="@+id/suburbanButton2"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="15dp"
                    android:text="Rural" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/urbanButton1"
                    android:text="Characteristics of Channel:"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignRight="@+id/suburbanButton2"
                    android:layout_below="@+id/textView2"
                    android:text="Medium Text"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <CheckBox
                    android:id="@+id/checkBox1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/textView3"
                    android:layout_toLeftOf="@+id/ruralButton3"
                    android:text="Manual Override" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/Power"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/Connectivity"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/Offline"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </RelativeLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>

MyTabsActivity.java :

package com.example.qosmetre;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;

public class Tabs extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);

        // we start adding the tabs programmatically
        TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);

        mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Network")
                .setContent(R.id.Network));
        mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Power")
                .setContent(R.id.Power));
        mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Connectivity")
                            .setContent(R.id.Connectivity));
        mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator("Offline")
                .setContent(R.id.Offline));

        mTabHost.setCurrentTab(0); // sets default tab
    }
    // more methods
}

在下面的评论中回答您的问题:

"i basically want 4 tabs with different names, and then in each of those tabs i want different textboxes, checkboxes and etc as well as graphs. Keep in mind that the tabs will not have the ame stuff inside them"

您正在以编程方式添加选项卡。如果你想附上整个 layout ,定义布局(例如在 LinearLayoutFrameLayout 等中),然后以编程方式将它们添加到您的 .java 中代码。

你的 tabs.xml具有以下结构:

从上面的结构图中可以看出,你的xml代码只有<RelativeLayout android:id="@+id/Network" </>中有对象(例如TextView、RadioButton等) .请在其他RelativeLayout中添加您想要显示的对象s(即电源、连接和离线)。

一个你为所有其他三个添加对象RelativeLayout s,您必须将设置的代码写入您的.java中的选项卡。代码。例如,以下代码设置 <RelativeLayout android:id="@+id/Networktab1 .

mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Network")
            .setContent(R.id.Network));

请重复上述操作并相应地添加所有四个选项卡。您可以替换 NetworkR.id.Network到其他选项卡名称(例如 PowerR.id.Power )。

关于android - 两个 XML 文件,但其中一个在运行时未显示在应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15355400/

有关android - 两个 XML 文件,但其中一个在运行时未显示在应用程序中的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  3. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  4. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  5. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  6. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  7. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  8. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  9. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  10. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

随机推荐