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

问题描述 投票:21回答:4

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

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

这是我的代码,我正在使用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" />
android android-layout
4个回答
31
投票

将它包裹在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>

17
投票

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

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

这对我有用。


1
投票

如果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"
     />

0
投票

如果您对RelativeLayout的观看次数如下:

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

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

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

4-将layout_gravity设置为center_vertical(或horizontal)到框架布局的子项

结果: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>

© www.soinside.com 2019 - 2024. All rights reserved.