代码之家  ›  专栏  ›  技术社区  ›  skvyas

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

  •  -1
  • skvyas  · 技术社区  · 16 年前

    我正在切换点击标签的活动,并在这方面取得了成功。但是,在我的一个活动课上,我正在做以下工作:

    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>
    

    我只想更改此布局的背景,我希望选项卡按原样显示。但是随着电流 android:layout_height="fill_parent" 在main.xml中,我的背景正在覆盖选项卡,这意味着我看不到选项卡。如果我做 android:layout_height="wrap_content" 我看不到任何变化,标签仍然是它们的。

    请帮忙。


    这是一些信息……
    屏幕2.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>
    

    屏幕1.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(主活动.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
    
        }
    }
    

    Java2.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有一个scrollview和iside,我有一个linearlayout,它有一个登录表单……但是每当调用这个表单时,它就出现在选项卡上方……请帮助

    1 回复  |  直到 8 年前
        1
  •  0
  •   pgsandstrom    16 年前

    你的标签有问题。单击选项卡时是否启动新活动?然后,如果这个新的活动布局覆盖了整个屏幕,那么这些选项卡当然也会被覆盖。

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