如何在Android布局中的同一列上对齐

发布于 2025-01-27 14:20:30 字数 2646 浏览 2 评论 0 原文

我有2个文本视图,我希望它们在同一列中水平对齐。我尝试 Android:layout_torightof =“@ID/textName” ,但似乎它变为底部,而是在 textName

moviedate 应在 moviename

输入图像描述描述这里

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_margin="10dp"
                android:src="@drawable/candy" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textName"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Name"
                    android:textColor="#000"
                    android:layout_marginRight="100dp"
                    android:textSize="15sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textdate"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Date"
                    android:textSize="15sp"
                    android:layout_toRightOf="@id/textName"/>


            </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>


</LinearLayout>

I have 2 text view and i want them to align horizontally at the same column. I tried to android:layout_toRightOf="@id/textName" but it seem like it goes to the bottom instead beside the textName

enter image description here

Expected output:
The MovieDate should align beside the MovieName

enter image description here

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_margin="10dp"
                android:src="@drawable/candy" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/textName"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Name"
                    android:textColor="#000"
                    android:layout_marginRight="100dp"
                    android:textSize="15sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textdate"
                    android:layout_width="50dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Date"
                    android:textSize="15sp"
                    android:layout_toRightOf="@id/textName"/>


            </LinearLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>


</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

筑梦 2025-02-03 14:20:30

layout_torightof relativelayout param,它是针对 linearlayout 的儿童设置的no-op设置,因此您可以安全地删除tihs line,它无能为力。 。

​我看到代码中有很多问题和潜在的改进,因此也许您还应该设置 android:layout_height =“ wrap_content” for此 linearlayout android:dravelity:dravelity: =“ center_vertical” for ofter One( cardView 的第一个孩子),还删除 android:layout_weight =“ 2” ,它的不必要,因为此布局具有 match_parent 宽度

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="50dp"
            android:layout_height="60dp"
            android:layout_margin="10dp"
            android:src="@drawable/candy" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textName"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Movie Name"
                android:textColor="#000"
                android:layout_marginRight="100dp"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textdate"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="Movie Date"
                android:textSize="15sp"/>

        </LinearLayout>

    </LinearLayout>

layout_toRightOf is an RelativeLayout param, it is no-op set for child of LinearLayout, so you can safely remove tihs line, it does nothing...

LinearLayout which is a parent of both TextViews should have set android:orientation="horizontal", thats for shure... but I see a lot of problems and potential improvements in your code, so maybe you should also set android:layout_height="wrap_content" for this LinearLayout and android:gravity="center_vertical" for outer one (first child of CardView), also remove android:layout_weight="2", its unnecessary as this layout have match_parent width

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/imageview"
            android:layout_width="50dp"
            android:layout_height="60dp"
            android:layout_margin="10dp"
            android:src="@drawable/candy" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textName"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Movie Name"
                android:textColor="#000"
                android:layout_marginRight="100dp"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textdate"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:text="Movie Date"
                android:textSize="15sp"/>

        </LinearLayout>

    </LinearLayout>
一向肩并 2025-02-03 14:20:30

使用Linearlayout并将定向设置为水平。请参阅此

Use LinearLayout and set orientation to horizontal instead . See this ????????

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
   xmlns:tools="http://schemas.android.com/tools" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android: orientation="horizontal" 
   tools:context=".MainActivity">
   <TextView android:layout_width="0dp" 
        android:layout_height="wrap_content"
        android:layout_weight="50" 
        android:gravity="center" 
        android:layout_gravity="center_vertical"
        android:text="Movie Name"/>
    <TextView android:layout_width="0dp" 
        android:layout_height="wrap_content"
        android:layout_weight="50" 
        android:gravity="center" 
        android:layout_gravity="center_vertical"
        android:text="Movie Date"/>
</LinearLayout>

The layout_weight attribute with value of 50 gives equal portions to each children i.e 50% each

小耗子 2025-02-03 14:20:30

我认为Linearlayout是最简单的解决方案,但是您也可以尝试使用ConstraintLayout。

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">

            <ImageView
                android:id="@+id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_marginEnd="10dp"
                android:src="@drawable/candy"
                app:layout_constraintEnd_toStartOf="@id/textName"
                app:layout_constraintHorizontal_chainStyle="spread"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/textName"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Movie Name"
                android:textColor="#000"
                android:textSize="15sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toStartOf="@id/textdate"
                app:layout_constraintStart_toEndOf="@id/imageview"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/textdate"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="100dp"
                android:text="Movie Date"
                android:textSize="15sp"
                app:layout_constraintStart_toEndOf="@+id/textName"
                app:layout_constraintTop_toTopOf="parent" />


        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

LinearLayout is the easiest solution in my opinion, but you could also try ConstraintLayout.

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">

            <ImageView
                android:id="@+id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_marginEnd="10dp"
                android:src="@drawable/candy"
                app:layout_constraintEnd_toStartOf="@id/textName"
                app:layout_constraintHorizontal_chainStyle="spread"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <TextView
                android:id="@+id/textName"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Movie Name"
                android:textColor="#000"
                android:textSize="15sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toStartOf="@id/textdate"
                app:layout_constraintStart_toEndOf="@id/imageview"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/textdate"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="100dp"
                android:text="Movie Date"
                android:textSize="15sp"
                app:layout_constraintStart_toEndOf="@+id/textName"
                app:layout_constraintTop_toTopOf="parent" />


        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>
慢慢从新开始 2025-02-03 14:20:30

而不是使用多个 Linearlayout ,您可以使用单个 ConstraintLayout 并实现相同的目标。这是修改的代码:

<?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_margin="10dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:src="@drawable/candy" />
                <TextView
                    android:id="@+id/textName"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Name"
                    android:textColor="#000"
                    android:layout_marginRight="100dp"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    app:layout_constraintTop_toTopOf="@id/imageview"
                    app:layout_constraintStart_toEndOf="@id/imageview"/>

                <TextView
                    android:id="@+id/textdate"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Date"
                    android:textSize="15sp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@id/textName"
                    app:layout_constraintStart_toEndOf="@id/textName"/>
        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

Instead of using multiple LinearLayout you can use single ConstraintLayout and achieve same goal. Here is the modified code:

<?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_margin="10dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:src="@drawable/candy" />
                <TextView
                    android:id="@+id/textName"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Name"
                    android:textColor="#000"
                    android:layout_marginRight="100dp"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    app:layout_constraintTop_toTopOf="@id/imageview"
                    app:layout_constraintStart_toEndOf="@id/imageview"/>

                <TextView
                    android:id="@+id/textdate"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="Movie Date"
                    android:textSize="15sp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@id/textName"
                    app:layout_constraintStart_toEndOf="@id/textName"/>
        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>
一曲琵琶半遮面シ 2025-02-03 14:20:30
// instead of Linearlayout use RelativeLayout and remove layout gravity.

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

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_margin="10dp"
                android:src="@drawable/googleg_disabled_color_18" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textName"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Movie Name"
                android:textColor="#000"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textdate"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/textName"
                android:text="Movie Date"
                android:layout_marginStart="15dp"
                android:textSize="15sp" />


        </RelativeLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>
// instead of Linearlayout use RelativeLayout and remove layout gravity.

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

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="5dp"
        app:cardBackgroundColor="#FFFFFF"
        app:cardCornerRadius="10dp"
        app:cardElevation="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview"
                android:layout_width="50dp"
                android:layout_height="60dp"
                android:layout_margin="10dp"
                android:src="@drawable/googleg_disabled_color_18" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textName"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Movie Name"
                android:textColor="#000"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textdate"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/textName"
                android:text="Movie Date"
                android:layout_marginStart="15dp"
                android:textSize="15sp" />


        </RelativeLayout>

    </LinearLayout>

</androidx.cardview.widget.CardView>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文