【问题标题】:How to get a textview centred on top of an ImageView如何使文本视图以 ImageView 为中心
【发布时间】:2013-07-19 18:12:15
【问题描述】:

我试图让一些文本出现在ImageView 的中心。但是使用这个 xml(如下),我看到它的文本楔入 FrameLayout 的左上角。我做错了什么?

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

        <ImageView
            android:id="@+id/levbut"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:scaleType="centerInside"
            android:src="@drawable/starbut"
             />

    <TextView
        android:id="@+id/tvtest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        />

</FrameLayout>

【问题讨论】:

  • 设置 android:gravity="center" of textView.
  • 没什么区别 :-(

标签: android android-imageview textview


【解决方案1】:

您可以向 FrameLayout 添加重力,向 textView 添加 layout_gravity,或者将其更改为 RelativeLayout 并添加 centerInParent="true"。

<FrameLayout
    ...
    android:gravity="center">

<FrameLayout
    ... />
    <TextView
        ...
        android:layout_gravity="center" />
</FrameLayout>

<ReltiveLayout
    ...>
    <ImageView ... />
    <TextView
        ...
        android:layout_centerInParent="true" />
</RelativeLayout>

【讨论】:

  • 将 android:layout_gravity="center" 添加到 TextView 就可以了
【解决方案2】:

android:layout_gravity="center" 更改为android:gravity="center" 作为FrameLayout 元素。

【讨论】:

    【解决方案3】:

    尝试为 TextView 添加 android:gravity="center" 并将其宽度和高度更改为匹配父项

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >
    
    <ImageView
        android:id="@+id/levbut"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scaleType="centerInside"
        android:src="@drawable/star_but" />
    
    <TextView
        android:id="@+id/tvtest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello" 
        android:gravity="center"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多