android - 将一个文本视图与另一个视图的中心对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12718853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:31:28  来源:igfitidea点击:

android - align a textview to the center of another view

androidandroid-layout

提问by Hairo

i have some imagebuttons and each one has a corresponding textview, i'd like to align those textview's to the center of their corresponding imageview, i mean, like is seen on the app drawer...

我有一些图像按钮,每个按钮都有一个相应的文本视图,我想将这些文本视图与其相应图像视图的中心对齐,我的意思是,就像在应用程序抽屉上看到的那样......

 !-----!
 !icon !
 !_____!
app  name

this is my code, i'm using a RelativeLayout

这是我的代码,我正在使用 RelativeLayout

<ImageButton
    android:id="@+id/blog_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/logo_img"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="51dp"
    android:background="@null"
    android:contentDescription="@string/blog_desc"
    android:src="@drawable/blog" />

<TextView
    android:id="@+id/blog_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/blog_button"
    android:layout_below="@+id/blog_button"
    android:layout_marginTop="8dp"
    android:text="@string/blog_desc" />

回答by Jason Robinson

Wrap that in a LinearLayoutand center the children, like so:

将其包裹在 a 中LinearLayout并将孩子居中,如下所示:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/logo_img"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <ImageButton
        android:id="@+id/blog_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:contentDescription="@string/blog_desc"
        android:src="@drawable/blog" />

    <TextView
        android:id="@+id/blog_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/blog_desc" />
</LinearLayout>

回答by gwakamol

Old post but if this can help I think it's not necessary to add an additional container.

旧帖子,但如果这可以帮助我认为没有必要添加额外的容器。

<ImageButton
    android:id="@+id/blog_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/logo_img"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="51dp"
    android:background="@null"
    android:contentDescription="@string/blog_desc"
    android:src="@drawable/blog" />

<TextView
    android:id="@+id/blog_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/blog_button"
    android:layout_alignRight="@+id/blog_button"
    android:layout_below="@+id/blog_button"
    android:gravity="center_horizontal"
    android:layout_marginTop="8dp"
    android:layout_marginLeft="-20dp"
    android:layout_marginRight="-20dp"
    android:text="@string/blog_desc" />

It's work for me.

这对我有用。

回答by Pratap Patil

If a the TextView can be transparent then this can be achieved with a single View

如果 TextView 可以是透明的,那么这可以通过单个 View 来实现

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test string"
    android:drawableTop="@android:drawable/btn_star"
    android:background="@android:color/transparent"
    android:textSize="10sp"
     />

回答by Amir Hossein Ghasemi

If your views in RelativeLayoutfollow these steps:

如果您的意见RelativeLayout遵循以下步骤:

1- wrap your view that wants to be aligned in the center of target view in Framelayout.

1- 将要在 Framelayout 中的目标视图中心对齐的视图包装起来。

2- move all layout attributes to FrameLayout.

2- 将所有布局属性移动到 FrameLayout。

3- set layout_align_topand layout_align_bottom(or start and end if horizontal) to target view.

3- 设置layout_align_toplayout_align_bottom(或开始和结束,如果水平)到目标视图。

4- set layout_gravityto center_vertical(or horizontal) to child of Frame layout

4-设置layout_gravitycenter_vertical(或horizontal)为框架布局的子项

Result: enter image description here

结果: 在此处输入图片说明

<RelativeLayout
        android:id="@+id/yourView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0">

    <RatingBar
            android:id="@+id/masseur_rating_bar"
            android:layout_width="wrap_content"
            android:layout_height="16dp"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:isIndicator="true"
            android:progressDrawable="@drawable/ic_selector_rating_bar"
            android:rating="@{masseurItem.rate}"
            android:scaleX="0.8"
            android:scaleY="0.8"
            tools:rating="4.5" />

    <TextView
            android:id="@+id/rate_text_view"
            style="@style/BodyTextViewStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@id/masseur_rating_bar"
            android:text="@{String.valueOf(masseurItem.rate)}"
            android:textSize="12sp"
            tools:text="4.5" />
</RelativeLayout>