简体   繁体   English

Android:标签顶部的布局

[英]Android: Layout on top of tabs

替代文字

As you can see, a linearlayout is on top of the tabs. 如您所见,线性布局位于选项卡的顶部。 What I want is to align the LinearLayout bottom to the tabs top. 我想要的是将LinearLayout底部与选项卡顶部对齐。

Alternatively aligning the RelativeLayout to the top of tabs may also work. 或者,也可以将RelativeLayout与选项卡的顶部对齐。

Here's the corresponding code for the view above: 这是上面视图的相应代码:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="wrap_content" android:id="@+id/LinearLayout02" android:layout_alignParentBottom="true">
        <FrameLayout android:id="@+id/FrameLayout02" android:layout_height="wrap_content" android:layout_width="fill_parent" android:paddingTop="20sp" android:paddingBottom="20sp">
            <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/record" android:layout_gravity="center_horizontal" android:id="@+id/RecordImageButton"></ImageButton>
        </FrameLayout>
        <SeekBar android:id="@+id/SeekBar01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:paddingBottom="5sp" android:paddingLeft="10sp" android:paddingRight="10sp"></SeekBar>
        <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/FrameLayout03">
            <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/LinearLayout02" android:layout_gravity="center_horizontal">
                <ImageButton android:layout_width="wrap_content" android:id="@+id/ImageButton01" android:layout_height="wrap_content" android:src="@drawable/play_rev"></ImageButton>
                <ImageButton android:layout_width="wrap_content" android:id="@+id/ImageButton02" android:layout_height="wrap_content" android:src="@drawable/play_stop"></ImageButton>
                <ImageButton android:layout_width="wrap_content" android:id="@+id/ImageButton03" android:layout_height="wrap_content" android:src="@drawable/play"></ImageButton>
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</RelativeLayout>

Here is the code for the tabs: 以下是标签的代码:

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs"
        android:layout_alignParentBottom="true" />

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

    </RelativeLayout>
</TabHost>

MainActivity, which sets an activity for each tab: MainActivity,它为每个选项卡设置一个活动:

public void initTabs()
{
    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    intent = new Intent().setClass(this, FirstActivity.class);
    spec = tabHost.newTabSpec("tab1").setIndicator("Tab",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, SecondActivity.class);
    spec = tabHost.newTabSpec("tab2").setIndicator("Tab 2",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, ThridActivity.class);
    spec = tabHost.newTabSpec("tab3").setIndicator("Tab 3",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

You can create params and rules : 您可以创建参数和规则:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)

params.addRule(RelativeLayout.Top, anotherView.getId());

This line means that one view bottom will be top of another... This can works but it is adviced to do that in xml. 这条线意味着一个视图的底部将在另一个视图的顶部。这可以工作,但建议在xml中进行。

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="fill_parent"
        android:orientation="vertical" android:id="@+id/LinearLayout02"
        android:layout_alignParentBottom="true" android:weightSum="1"
        android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/LinearLayout03"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:layout_weight=".25" android:weightSum="1"
            android:orientation="vertical">
            <FrameLayout android:id="@+id/FrameLayout02"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:paddingTop="20sp" android:paddingBottom="20sp"
                android:layout_weight=".5">
                <ImageButton android:layout_width="wrap_content"
                    android:layout_height="wrap_content" android:src="@drawable/record"
                    android:layout_gravity="center_horizontal" android:id="@+id/RecordImageButton"></ImageButton>
            </FrameLayout>
            <SeekBar android:id="@+id/SeekBar01" android:layout_height="wrap_content"
                android:layout_width="fill_parent" android:paddingBottom="5sp"
                android:paddingLeft="10sp" android:paddingRight="10sp"></SeekBar>
            <FrameLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:id="@+id/FrameLayout03">
                <LinearLayout android:layout_height="wrap_content"
                    android:layout_width="wrap_content" android:id="@+id/LinearLayout02"
                    android:layout_gravity="center_horizontal">
                    <ImageButton android:layout_width="wrap_content"
                        android:id="@+id/ImageButton01" android:layout_height="wrap_content"
                        android:src="@drawable/play_rev"></ImageButton>
                    <ImageButton android:layout_width="wrap_content"
                        android:id="@+id/ImageButton02" android:layout_height="wrap_content"
                        android:src="@drawable/play_stop"></ImageButton>
                    <ImageButton android:layout_width="wrap_content"
                        android:id="@+id/ImageButton03" android:layout_height="wrap_content"
                        android:src="@drawable/play"></ImageButton>
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
        <LinearLayout android:id="@+id/TabLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight=".75">
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

You can add your TabHost things into the TabLayout(last LinearLayout). 您可以将TabHost东西添加到TabLayout(last LinearLayout)中。 Hope that will solve your problem. 希望能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM