Android 中心文本视图,带可拉拔顶部的线性布局

Android 中心文本视图,带可拉拔顶部的线性布局,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,这是目前的情况 我已经阅读了这里提供的其他解决方案,但没有一个真正有用。我想将这4个文本视图居中,根据线性布局的水平和垂直中心可绘制。在移动设备上,一切正常,但在平板电脑上,底部栏的高度更高,我希望控件居中 代码如下: <LinearLayout android:id="@+id/bottomBar" android:layout_width="match_parent" android:layout_height="

这是目前的情况

我已经阅读了这里提供的其他解决方案,但没有一个真正有用。我想将这4个文本视图居中,根据线性布局的水平和垂直中心可绘制。在移动设备上,一切正常,但在平板电脑上,底部栏的高度更高,我希望控件居中

代码如下:

<LinearLayout
            android:id="@+id/bottomBar"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".2"
            android:orientation="horizontal">

            <com.dyve.counting.widget.DVTextView
                android:id="@+id/txtAreaTools"
                style="@style/BottomBarButton"
                android:layout_weight="0.19"
                android:background="@drawable/bottom_bar_back_with_arrow"
                android:drawableTop="@drawable/area_tool"
                android:textColor="@color/white"
                android:gravity="center_horizontal"
                android:textSize="@dimen/bottom_bar_text_size"
                android:text="@string/area" />

            <com.dyve.counting.widget.DVTextView
                android:id="@+id/clearAllTool"
                style="@style/BottomBarButton"
                android:layout_weight="0.19"
                android:textColor="@color/white"
                android:gravity="center_horizontal"
                android:textSize="@dimen/bottom_bar_text_size"
                android:background="@drawable/bottom_bar_back"
                android:visibility="gone"/>

            <FrameLayout
                android:layout_width="0dp"
                android:layout_weight="0.19"
                android:layout_height="match_parent">
                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center">

                    <com.dyve.counting.widget.DVTextView
                        android:id="@+id/txtToggleNumbers"
                        style="@style/BottomBarButton"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:textSize="@dimen/bottom_bar_text_size"
                        android:textColor="@color/white"
                        android:gravity="center_horizontal"
                        android:drawableTop="@drawable/hide_numbers_tool" />

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">
                        <com.dyve.counting.widget.DVTextView
                            android:id="@+id/mCalibrateToolView"
                            style="@style/BottomBarButton"
                            android:layout_centerHorizontal="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center_horizontal"
                            android:textColor="@color/white"
                            android:gravity="center_horizontal"
                            android:textSize="@dimen/bottom_bar_text_size"
                            android:background="@drawable/bottom_bar_back_with_arrow"
                            android:drawableTop="@drawable/show_calibrate_tool"
                            android:text="@string/calibrate_text" />

                        <TextView
                            android:id="@+id/badgeRadius"
                            android:layout_width="@dimen/calibrate_size"
                            android:layout_height="@dimen/calibrate_size"
                            android:textColor="@color/primary_color"
                            android:textStyle="bold"
                            android:gravity="center"
                            android:layout_alignRight="@+id/mCalibrateToolView"
                            android:layout_marginRight="@dimen/calibrate_margin"
                            android:layout_marginTop="@dimen/calibrate_margin_top"
                            android:background="@drawable/shape_notification"/>
                    </RelativeLayout>
                </FrameLayout>
            </FrameLayout>

            <com.dyve.counting.widget.DVTextView
                android:id="@+id/btnCount"
                android:layout_width="0dp"
                android:layout_weight="0.24"
                android:layout_height="match_parent"
                android:textColor="@color/white"
                style="@style/AreaToolButton"
                android:textSize="18dp"
                android:background="#33FFFFFF"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:text="@string/count_text" />

            <com.dyve.counting.widget.DVTextView
                android:id="@+id/txtTagTools"
                style="@style/BottomBarButton"
                android:layout_weight="0.19"
                android:textColor="@color/white"
                android:textSize="@dimen/bottom_bar_text_size"
                android:background="@drawable/bottom_bar_back_with_arrow"
                android:drawableTop="@drawable/tags_tool"
                android:gravity="center_horizontal"
                android:text="@string/tags" />

            <com.dyve.counting.widget.DVTextView
                android:id="@+id/menuTool"
                style="@style/BottomBarButton"
                android:layout_weight="0.19"
                android:textColor="@color/white"
                android:drawableTop="@drawable/bottom_more_count"
                android:gravity="center_horizontal"
                android:textSize="@dimen/bottom_bar_text_size"
                android:text="@string/more" />
        </LinearLayout>

感谢您的帮助。谢谢。

尝试添加:

android:layout_height="wrap_content"
在你的信中:

<com.dyve.counting.widget.DVTextView
      android:id="@+id/txtAreaTools"
      style="@style/BottomBarButton"
      android:layout_weight="0.19"
      android:background="@drawable/bottom_bar_back_with_arrow"
      android:drawableTop="@drawable/area_tool"
      android:textColor="@color/white"
      android:gravity="center_horizontal"
      android:textSize="@dimen/bottom_bar_text_size"
      android:text="@string/area" />


将您的
android:gravity=“center\u horizontal”
更改为
android:gravity=“center”
请尝试用以下代码替换您的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bottomBar"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".2"
    android:orientation="horizontal">

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <com.dyve.counting.widget.DVTextView
            android:id="@+id/txtAreaTools"
            style="@style/BottomBarButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/bottom_bar_back_with_arrow"
            android:drawableTop="@drawable/area_tool"
            android:gravity="center"
            android:text="@string/area"
            android:textColor="@color/white"
            android:textSize="@dimen/bottom_bar_text_size" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <com.dyve.counting.widget.DVTextView
            android:id="@+id/clearAllTool"
            style="@style/BottomBarButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/bottom_bar_back"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="@dimen/bottom_bar_text_size"
            android:visibility="gone" />
    </FrameLayout>


    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

            <com.dyve.counting.widget.DVTextView
                android:id="@+id/txtToggleNumbers"
                style="@style/BottomBarButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:drawableTop="@drawable/hide_numbers_tool"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="@dimen/bottom_bar_text_size" />

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center">

                <com.dyve.counting.widget.DVTextView
                    android:id="@+id/mCalibrateToolView"
                    style="@style/BottomBarButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:background="@drawable/bottom_bar_back_with_arrow"
                    android:drawableTop="@drawable/show_calibrate_tool"
                    android:gravity="center"
                    android:text="@string/calibrate_text"
                    android:textColor="@color/white"
                    android:textSize="@dimen/bottom_bar_text_size" />

                <TextView
                    android:id="@+id/badgeRadius"
                    android:layout_width="@dimen/calibrate_size"
                    android:layout_height="@dimen/calibrate_size"
                    android:layout_alignRight="@+id/mCalibrateToolView"
                    android:layout_marginRight="@dimen/calibrate_margin"
                    android:layout_marginTop="@dimen/calibrate_margin_top"
                    android:background="@drawable/shape_notification"
                    android:gravity="center"
                    android:textColor="@color/primary_color"
                    android:textStyle="bold" />
            </RelativeLayout>
        </FrameLayout>
    </FrameLayout>


    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.9">


        <com.dyve.counting.widget.DVTextView
            android:id="@+id/btnCount"
            style="@style/AreaToolButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:background="#33FFFFFF"
            android:gravity="center"
            android:text="@string/count_text"
            android:textColor="@color/white"
            android:textSize="18dp" />
    </FrameLayout>


    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <com.dyve.counting.widget.DVTextView
            android:id="@+id/txtTagTools"
            style="@style/BottomBarButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/bottom_bar_back_with_arrow"
            android:drawableTop="@drawable/tags_tool"
            android:gravity="center"
            android:text="@string/tags"
            android:textColor="@color/white"
            android:textSize="@dimen/bottom_bar_text_size" />
    </FrameLayout>


    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <com.dyve.counting.widget.DVTextView
            android:id="@+id/menuTool"
            style="@style/BottomBarButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:drawableTop="@drawable/bottom_more_count"
            android:gravity="center"
            android:text="@string/more"
            android:textColor="@color/white"
            android:textSize="@dimen/bottom_bar_text_size" />
    </FrameLayout>
</LinearLayout>

编辑::

用一个框架布局包装所有视图,其权重超过该框架布局..因此您的内部视图不会扩展太多。。。 尝试新编辑的代码


请告诉我是否需要更多帮助。

在顶层
线性布局
android:gravity=“center\u vertical”
不起作用?不幸的是,没有。没有视觉上的区别您尝试过的ID
layout\u gravity
?是的,同样的结果,没有区别。下面是它的外观()这个想法是图标和文本垂直居中,而现在文本的位置非常低。如果底部栏的权重更高,图像和文本之间的距离将更大。不起作用。下面是它现在的样子()想法是图标和文本垂直居中,而现在文本的位置非常低。如果底部栏的权重更高,则图像和文本之间的距离将更大。@ZackKaytranada我无法看到您在注释中链接的图像,因此我无法了解上述更改后发生的情况。只有文本垂直居中,图像位于视图的上方,因为您将图像设置为android:drawableTop=“@drawable/bottom\u more\u count”
drawableTop
意味着它将保持在顶部。