android - Tabcontent 应始终与 RelativeLayout 对齐

标签 android xml android-layout

我在 LinearLayout 中有三个 3 Relative 布局。 第一个相对布局包含 Tabhost。 所有 Tabhost 内容都应始终在第二个 RelativeLayout 上方对齐。 问题是内容总是被视为第三和第二相对布局的后面。 它如何始终保持在第二个 RelativeLayout 之上?

enter image description here

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@color/background_floating_material_dark">

    <android.support.v4.app.FragmentTabHost
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFFFF">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:gravity="top"/>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="62dp"/>

    </android.support.v4.app.FragmentTabHost>

    <RelativeLayout
        android:id="@+id/rel_for_sip_active"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#19c031"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_above="@+id/relativeLayout"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Online"
            android:id="@+id/online_offline"
            android:layout_marginLeft="39dp"
            android:layout_marginStart="39dp"
            android:textColor="#afafaf"
            android:textStyle="bold"
            android:layout_marginTop="15dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="On going call....."
            android:id="@+id/textView12"
            android:layout_marginLeft="39dp"
            android:layout_marginStart="39dp"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:visibility="gone"
            android:layout_marginTop="15dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="00:00:00"
            android:id="@+id/callcounter"
            android:textColor="#afafaf"
            android:textStyle="bold"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/unmute"
            android:onClick="Unmute"
            android:src="@mipmap/ic_action_mute"
            android:visibility="gone"
            android:background="@android:color/transparent"
            android:layout_marginRight="26dp"
            android:layout_marginEnd="26dp"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/mute"
            android:onClick="Mute"
            android:src="@mipmap/ic_action_mute_active"
            android:background="@android:color/transparent"
            android:visibility="gone"
            android:layout_marginRight="26dp"
            android:layout_marginEnd="26dp" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/demute"
            android:src="@mipmap/ic_action_mute_inactive"
            android:background="@android:color/transparent"
            android:visibility="visible"
            android:layout_marginRight="26dp"
            android:layout_marginEnd="26dp"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/background_floating_material_dark"
        style="@style/Theme.Callbox"
        android:id="@+id/relativeLayout"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:id="@+id/action_bar_addtarget"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_action_call"
            android:background="@drawable/bg_btn_btm_actionbar"
            android:visibility="visible"
            android:onClick="Call" />

        <ImageView
            android:id="@+id/action_bar_addtarget_end"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:scaleType="fitCenter"
            android:background="@drawable/bg_btn_btm_actionbar"
            android:src="@drawable/ic_action_end_call"
            android:visibility="gone"
            android:onClick="endCall" />

        <ImageView
            android:id="@+id/action_bar_dialpad"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_action_dialer"
            android:background="@drawable/bg_btn_btm_actionbar"
            android:visibility="visible"
            android:onClick="showDialpad"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <ImageView
            android:id="@+id/action_bar_filter"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:contentDescription="@string/dummy"
            android:background="@drawable/bg_btn_btm_actionbar"
            android:visibility="visible"
            android:scaleType="fitCenter"
            android:onClick="showCallResult"
            android:src="@drawable/ic_action_process_save"
            android:layout_below="@+id/action_bar_addtarget_end"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</RelativeLayout>

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/relativeLayout"
    android:visibility="gone"
    android:id="@+id/fl_slide_pop">
</FrameLayout>

最佳答案

您的设计中有几处不好。 您的布局太多,我尝试尽可能多地删除其中一些布局。

还有 3 个 ImageButtons 用于静音/取消静音/静音,其中只有一个可以设置或以编程方式更改。

如果你不优化它,膨胀 View 在 Android 上有点繁重。

有一个tool进入 SDK,帮助您查看布局层次结构,以便了解如何对其进行优化 你可以尝试做一些更简单的事情,比如这个例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!-- here is the bottom layout which contains your 3 buttons -->
    <LinearLayout
        android:id="@+id/third_layout"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="#ff0000"
        android:orientation="horizontal"
        android:weightSum="3">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />


    </LinearLayout>


    <!-- Here is the equivalent of the 2nd relative layout, but you just have to show a image 
    button or a Text view, you don't have to use another relative layout -->
    <ImageButton
        android:id="@+id/mute"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_above="@id/third_layout"
        android:background="@android:color/holo_green_dark"
        android:src="@android:drawable/ic_btn_speak_now" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_above="@id/third_layout"
        />

    <!-- finally the 1st layout which is above the second to the top of its parent -->

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/mute"
        android:layout_alignParentTop="true"
        android:background="@android:color/holo_blue_bright">

        <!-- TAB Widget -->

    </android.support.v4.app.FragmentTabHost>


</RelativeLayout>

我鼓励您使用更少的布局以获得更流畅的应用程序。

关于android - Tabcontent 应始终与 RelativeLayout 对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32433555/

相关文章:

安卓 Kotlin 。使用 fragment 将数据绑定(bind)到 recyclerview 时出现问题

android - "No XML content. Please add a root view or layout to your document"

java - Android 的 PreferenceScreen 替代方案

android - 如何在网格布局中将文本恰好对齐在图像下方,并在超过图像宽度时使其占据第二行?

android - 拖放 imageview android

java - 使用RegEx在html源中查找特定字符串

android - 从 PendingIntent.getActivity Android 获取空指针异常

java - 我如何更新 ListItem (LiveData) 的字段

Android SAX 解析 : How to Preserve Newlines from within a CDATA Tag

android - 类似 Chrome 的菜单显示