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

Hai*_*iro 21 android android-layout

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

 !-----!
 !icon !
 !_____!
app  name
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我正在使用 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" />
Run Code Online (Sandbox Code Playgroud)

Jas*_*son 32

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>
Run Code Online (Sandbox Code Playgroud)

  • Jason,为了将来参考,[`FrameLayout`](http://developer.android.com/reference/android/widget/FrameLayout.html)通常不应该用于持有多个孩子.在这种情况下,`LinearLayout`将是最佳替代品. (3认同)
  • @Eric为什么?`FrameLayout`是实现Hairo想要的好方法.`LinearLayout`不起作用,因为你不能重叠儿童视图.另一种`ViewGroup`可能是`RelativeLayout`. (2认同)
  • @Hairo道歉.在重读了你的问题之后,我确实误解了你想要达到的目标.Eric是正确的,在这种情况下,最好的解决方案是`LinearLayout`. (2认同)

小智 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" />
Run Code Online (Sandbox Code Playgroud)

这对我有用.