单击选项卡后如何设置新活动的背景颜色

发布于 2024-09-03 00:14:58 字数 5634 浏览 2 评论 0原文

我正在通过点击选项卡来切换活动,并且成功了。但是,在我的 Activity 类之一中,我正在执行以下操作:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

main.xml 具有以下内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        android:background="#BDBDBD">
</LinearLayout>

我只想更改此布局的背景,并且我希望选项卡保持原样。但在 main.xml 中当前的 android:layout_height="fill_parent" 中,我的背景正在覆盖选项卡,这意味着我无法看到选项卡。如果我进行 android:layout_height="wrap_content" 我看不到任何更改,选项卡仍然是他们的。

请帮忙。


这是一些信息..
screen2.xml

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

    <LinearLayout 
        android:id="@+id/LinearLayout01" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <LinearLayout android:id="@+id/LinearLayout02" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:gravity="center">
            <TextView android:text="AcctId" 
                android:id="@+id/TextView01" 
                android:layout_width="50px" 
                android:layout_height="wrap_content"></TextView>
            <EditText 
                android:id="@+id/acctid" 
                android:layout_width="200px" 
                android:layout_height="wrap_content"></EditText>

        </LinearLayout>
        <LinearLayout android:id="@+id/LinearLayout03" 
            android:layout_width="fill_parent" 
            android:gravity="center"
            android:layout_height="wrap_content">
            <TextView android:text="Zip" 
                android:id="@+id/TextView02" 
                android:layout_width="50px" 
                android:layout_height="wrap_content">
            </TextView>
            <EditText 
                android:id="@+id/stid" 
                android:layout_width="200px" 
                android:layout_height="wrap_content">
            </EditText>
        </LinearLayout>
        <Button android:text="Login"
            android:layout_width="80px"
            android:layout_height="wrap_content"
            android:layout_gravity="center">
        </Button>
        <ImageView 
            android:layout_width="200px"
            android:layout_height="200px"
            android:src="@drawable/icon"
            android:layout_gravity="center">
        </ImageView>
    </LinearLayout>
</ScrollView>

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

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#c6c3c6"
        android:layout_gravity="bottom"
        android:paddingTop="1dip"
        android:paddingLeft="1dip"
        android:paddingRight="1dip">

    <TabWidget
        android:layout_gravity="bottom"
        android:id="@android:id/tabs"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"/>
    </FrameLayout>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    </FrameLayout>
</TabHost>  

MainActivity.java

public class onstarAndroidMain extends TabActivity {

    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);
    addTabs();
    }

    private void addTabs(){
    Resources resources = getResources();

    TabHost tabs = (TabHost) findViewById(android.R.id.tabhost);
    tabs.setup();

    //tabspec defines the attributes of the tab
    TabSpec tab;


    tab = tabs.newTabSpec("Screen1");
    tab.setContent(new Intent(this, Screen1.class));
    tab.setIndicator("Screen1");
    tabs.addTab(tab);
    tabs.getTabWidget().getChildAt(0).getLayoutParams().height = 30;

    tab = tabs.newTabSpec("Screen2");
    tab.setContent(new Intent(this, Screen2.class));
    tab.setIndicator("Screen2");
    tabs.addTab(tab);
    tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 30;

    tab = tabs.newTabSpec("Screen3");
    tab.setContent(new Intent(this, Screen3.class));
    tab.setIndicator("Screen3");
    tabs.addTab(tab);
    tabs.getTabWidget().getChildAt(2).getLayoutParams().height = 30;

    //sets the current tab
    tabs.setCurrentTab(0);
    //add the tabhost to the main content view

    }
}

Screen2.java

public class OnstarScreen2 extends Activity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.screen2);
}

现在,每当我单击 screen2 选项卡并尝试获取 screen2.xml 时,我的选项卡都会位于 screen2 布局下方。我不知道该怎么办。基本上我想要实现的是,选项卡应始终位于底部,无论其上方显示什么视图。它们不应与其他任何内容重叠。

screen2.xml 有一个滚动视图,旁边有一个线性布局,其中有一个登录表单...但是每当调用此表单时,它就出现在选项卡上方...请帮助

I am switching activities on tab clicks and successful at this. But, in one of my Activity class I am doing the following:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

main.xml has the following:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        android:background="#BDBDBD">
</LinearLayout>

I want to change the background of this layout only and I want tabs to their as it is. But with the current android:layout_height="fill_parent" in main.xml my background is overwriting the tabs which means I am unable to see tabs. and If I make android:layout_height="wrap_content" I cannot see any change taking and tabs are still their.

Please help.


Here is some information..
screen2.xml

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

    <LinearLayout 
        android:id="@+id/LinearLayout01" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <LinearLayout android:id="@+id/LinearLayout02" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:gravity="center">
            <TextView android:text="AcctId" 
                android:id="@+id/TextView01" 
                android:layout_width="50px" 
                android:layout_height="wrap_content"></TextView>
            <EditText 
                android:id="@+id/acctid" 
                android:layout_width="200px" 
                android:layout_height="wrap_content"></EditText>

        </LinearLayout>
        <LinearLayout android:id="@+id/LinearLayout03" 
            android:layout_width="fill_parent" 
            android:gravity="center"
            android:layout_height="wrap_content">
            <TextView android:text="Zip" 
                android:id="@+id/TextView02" 
                android:layout_width="50px" 
                android:layout_height="wrap_content">
            </TextView>
            <EditText 
                android:id="@+id/stid" 
                android:layout_width="200px" 
                android:layout_height="wrap_content">
            </EditText>
        </LinearLayout>
        <Button android:text="Login"
            android:layout_width="80px"
            android:layout_height="wrap_content"
            android:layout_gravity="center">
        </Button>
        <ImageView 
            android:layout_width="200px"
            android:layout_height="200px"
            android:src="@drawable/icon"
            android:layout_gravity="center">
        </ImageView>
    </LinearLayout>
</ScrollView>

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

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#c6c3c6"
        android:layout_gravity="bottom"
        android:paddingTop="1dip"
        android:paddingLeft="1dip"
        android:paddingRight="1dip">

    <TabWidget
        android:layout_gravity="bottom"
        android:id="@android:id/tabs"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"/>
    </FrameLayout>
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

    </FrameLayout>
</TabHost>  

MainActivity.java

public class onstarAndroidMain extends TabActivity {

    public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen1);
    addTabs();
    }

    private void addTabs(){
    Resources resources = getResources();

    TabHost tabs = (TabHost) findViewById(android.R.id.tabhost);
    tabs.setup();

    //tabspec defines the attributes of the tab
    TabSpec tab;


    tab = tabs.newTabSpec("Screen1");
    tab.setContent(new Intent(this, Screen1.class));
    tab.setIndicator("Screen1");
    tabs.addTab(tab);
    tabs.getTabWidget().getChildAt(0).getLayoutParams().height = 30;

    tab = tabs.newTabSpec("Screen2");
    tab.setContent(new Intent(this, Screen2.class));
    tab.setIndicator("Screen2");
    tabs.addTab(tab);
    tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 30;

    tab = tabs.newTabSpec("Screen3");
    tab.setContent(new Intent(this, Screen3.class));
    tab.setIndicator("Screen3");
    tabs.addTab(tab);
    tabs.getTabWidget().getChildAt(2).getLayoutParams().height = 30;

    //sets the current tab
    tabs.setCurrentTab(0);
    //add the tabhost to the main content view

    }
}

Screen2.java

public class OnstarScreen2 extends Activity {

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.screen2);
}

Now whenever I click the screen2 tab and try to get the screen2.xml, my tabs are going below the screen2 layout. I dont know what to do. Basically what I want to achieve is that the tab should be always their at the bottom irrespective of what view is shown above them. they should not be overlapped with anything else.

screen2.xml has a scrollview and iside that I have a linearlayout which has a login form...but this form whenever is called just comes above the tabs...please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2024-09-10 00:14:58

你的标签做错了。单击该选项卡时您是否刚刚启动一个新活动?然后,如果新的活动布局覆盖整个屏幕,选项卡当然也会被覆盖。

请遵循本指南:
http://developer.android.com/intl/ de/resources/tutorials/views/hello-tabwidget.html

You are doing something wrong with your tabs. Are you just launching a new activity when the tab is clicked? Then, if that new activitys layout covers the entire screen, ofcourse the tabs will be covered by this.

Follow this guide:
http://developer.android.com/intl/de/resources/tutorials/views/hello-tabwidget.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文