繁体   English   中英

在 LinearLayout 中使用 drawableTop 居中 TextView

[英]Center TextView with drawableTop in LinearLayout

这是目前的情况。 在此处输入图片说明

我已经阅读了这里提供的其他解决方案,但没有一个真正有帮助。 我想将这 4 个 textview 居中,它可以在 LinearLayout 的水平和垂直中心绘制。 在移动设备上一切正常,但在平板电脑上,底部栏的高度更高,我希望控件居中。

这是代码:

<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_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>

编辑::

用一个框架布局包裹所有视图,重量超过那个......所以你的内部视图不会扩展那么多......尝试新的编辑代码..

请告诉是否需要更多帮助..

暂无
暂无

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

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