android-如果textview值超过2个字符,一切都将向右移动

我有recyclerview,为此,我使用一个特定的视图.在该视图中,我有3个textview和一个图像按钮.第一个textview显示数字.其他一些伪文本.当我的数字超过10(2个字符)时,除数字以外的所有字符都向右移动.我该如何处理?

这是我的xml文件:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp">

        <LinearLayout
            android:id="@+id/linlay"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/buses_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:textSize="25dp"
                android:textColor="@color/gradStop"
                android:layout_marginLeft="15dp"/>

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

                <TextView
                    android:id="@+id/buses_route_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="30dp"
                    android:text="Bus Route"
                    android:textColor="@color/gradStart"
                    android:textSize="20dp"/>

                <TextView
                    android:id="@+id/buses_cycle_duration"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="30dp"
                    android:text="Duration"
                    android:textColor="@color/gradStop"
                    android:textSize="20dp"/>

            </LinearLayout>

            <ImageButton
                android:id="@+id/buses_favourites"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@drawable/ic_favorite_border_black_24dp"
                android:background="@color/transparent"
                android:layout_alignParentRight="true"/>

        </LinearLayout>

    </RelativeLayout>


</RelativeLayout>

这是屏幕截图,看起来像:

enter image description here

最佳答案
由于您使用的是线性布局,请尝试提供layout_weight

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="70dp">

        <LinearLayout
            android:id="@+id/linlay"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:orientation="horizontal"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/buses_number"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="1"
                android:layout_weight = "0.3"
                android:textSize="25dp"
                android:textColor="@color/gradStop"
                android:layout_marginLeft="15dp"/>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_weight = "0.6"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/buses_route_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="30dp"
                    android:text="Bus Route"
                    android:textColor="@color/gradStart"
                    android:textSize="20dp"/>

                <TextView
                    android:id="@+id/buses_cycle_duration"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="30dp"
                    android:text="Duration"
                    android:textColor="@color/gradStop"
                    android:textSize="20dp"/>

            </LinearLayout>

            <ImageButton
                android:id="@+id/buses_favourites"
                android:layout_width="0dp"
                android:layout_weight = "0.1"
                android:layout_height="30dp"
                android:src="@drawable/ic_favorite_border_black_24dp"
                android:background="@color/transparent"
                android:layout_alignParentRight="true"/>

        </LinearLayout>

    </RelativeLayout>


</RelativeLayout>

如您所见,TextView将占用30%的屏幕宽度空间,Other(LinearLayout)将占用60%的屏幕宽度,ImageView将占10%的空间.我相信您在掌握要点时可以根据自己的设计改变重量:)

相关文章

AdvserView.java package com.earen.viewflipper; import an...
ImageView的scaleType的属性有好几种,分别是matrix(默认)...
文章浏览阅读8.8k次,点赞9次,收藏20次。本文操作环境:win1...
文章浏览阅读1.2w次,点赞15次,收藏69次。实现目的:由main...
文章浏览阅读3.8w次。前言:最近在找Android上的全局代理软件...
文章浏览阅读2.5w次,点赞17次,收藏6次。创建项目后,运行项...