android - 如何在不与其他 View 冲突的情况下在布局中心对齐 textView?

标签 android xml android-layout android-studio android-linearlayout

我在尝试将 textView 与布局中心对齐时遇到问题:当我尝试这样做时 - 它与左侧的按钮有冲突 - 如果文本太长 - 它会隐藏在按钮下。文本长度可以有很大不同,我需要它在布局的中心,而不是在按钮下方/上方。

这是 Activity 布局的xml文件:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item_content_action_bar_layout"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="@color/colorBlue"
        android:padding="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_content_title"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Title"
            android:textColor="@color/colorWhite"
            android:textAlignment="center"
            android:singleLine="true"
            android:layout_gravity="center_vertical"
            android:textStyle="bold"
            android:autoText="false"
            android:layout_centerInParent="true"
            android:layout_alignWithParentIfMissing="false"
            android:layout_alignParentRight="false" />

        <ImageButton
            android:layout_width="120dp"
            android:layout_height="45dp"
            android:id="@+id/item_content_back_button"
            android:background="@drawable/back_button_selector"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginRight="5dp" />

    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/item_content_scroll_view"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <ImageView
                    android:layout_width="125dp"
                    android:layout_height="125dp"
                    android:id="@+id/item_content_image"
                    android:src="@drawable/ic_no_thumbnail"
                    android:maxHeight="125dp"
                    android:maxWidth="125dp"
                    android:layout_alignParentLeft="false"
                    android:layout_alignParentStart="true"
                    android:layout_margin="10dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/item_content_subtitle"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="Subtitle"
                    android:layout_alignTop="@+id/item_content_image"
                    android:layout_toRightOf="@+id/item_content_image"
                    android:layout_toEndOf="@+id/item_content_image"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/item_content_pubdate"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Date"
                    android:layout_below="@+id/item_content_subtitle"
                    android:layout_alignLeft="@+id/item_content_subtitle"
                    android:layout_alignStart="@+id/item_content_subtitle"
                    android:textSize="10dp"
                    android:layout_marginTop="10dp" />

            </RelativeLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/item_content_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="blabla"
                android:layout_below="@+id/item_content_pubdate"
                android:layout_alignStart="@+id/item_content_pubdate"
                android:layout_alignParentStart="true"
                android:layout_margin="10dp" />

        </LinearLayout>
    </ScrollView>

</LinearLayout>

最佳答案

为此尝试使用 LinearLayout with layout-weight 以避免冲突。将您的第一个相对布局更改为以下

<LinearLayout
        android:id="@+id/item_content_action_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/_white"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="7">

        <ImageButton
            android:id="@+id/item_content_back_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="2"
            android:background="@drawable/chemist" />

        <TextView
            android:id="@+id/item_content_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="5"
            android:autoText="false"
            android:gravity="center"
            android:maxLines="2"
            android:singleLine="true"
            android:text="Title"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/Black"
            android:textStyle="bold" />

    </LinearLayout>

关于android - 如何在不与其他 View 冲突的情况下在布局中心对齐 textView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36280321/

相关文章:

xml - 如何使用马提尼在 golang 中动态呈现为 HTML/JSON/XML?

java - 为什么 FAB 在不同的 Android 设备/操作系统版本上显示不同?

android - 在 Android 上为自定义形状添加阴影

Android:ViewBinding 的 ScrollView 内没有可用的 View ID。如何访问?

android - 如何从Android应用程序中的任何网址流式传输视频?

php - 在 PHP 中使用 XSLT 转换 XML

java - 如何在后按时执行某些操作,而不是仅针对一个 fragment 从 backStack 弹出,并且仍然能够对其他 fragment 正常工作?

android - 从 BottomSheetDialog 获取 BottomSheetBehaviour

java - Android MediaPlayer 可以播放压缩文件中的音频吗?

c# - 创建一个没有属性和第一个元素的 XmlDocument