繁体   English   中英

在TextView下方将TextView居中

[英]Center TextView below ImageView

我在XML的ImageView下方将TextView居中时遇到一些问题,因此我很好奇是否有可能。

这是一些示例代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

--- Lots of Code ---

    <RelativeLayout
        android:id="@+id/swipe_container_seekBar"
        android:layout_width="match_parent"
        android:layout_height="120dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/seekbar_leftball"
            android:layout_below="@id/seekbar_leftball"
            android:text="@string/swipe_filter_seekBar_job"
            android:textAlignment="center" />

        <ImageView
            android:id="@+id/seekbar_leftball"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_marginLeft="36dp"
            android:src="@drawable/seekbar_ball"
            android:tint="@color/red"/>
    </RelativeLayout>
</LinearLayout>

如您所见,我可以将其固定在ImageView的左侧,但无论如何我都无法找到它的中心。

这很简单 :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"   
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true">
            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/imageView5" android:src="@android:drawable/ic_menu_camera"
                    android:layout_gravity="center"/>
            <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="New Text"
                    android:id="@+id/textView5" android:gravity="center"/>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

结果:

在此处输入图片说明

给出的解决方案中有太多无用的视图。 试试这个吧。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView5"
        android:src="@android:drawable/ic_menu_camera"
        android:layout_centerInParent="true"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView5" 
        android:layout_below="@id/imageView5"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

在此处输入图片说明

将文字视图与可绘制对象一起使用

yourextView.setCompoundDrawablesWithIntrinsicBounds(_left, _top, _right, _bottom);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@mipmap/ic_launcher"
     />
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
      android:gravity="center"
        android:layout_height="wrap_content"
       android:layout_centerInParent="true">
    <ImageView
        android:id="@+id/logo2"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
android:layout_below="@+id/logo2"

        android:text="AAAAAAA"
        android:textStyle="bold"
/>
    </LinearLayout>
</RelativeLayout>

暂无
暂无

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

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