代码之家  ›  专栏  ›  技术社区  ›  CoolMind

两个文本视图应占据最小高度

  •  0
  • CoolMind  · 技术社区  · 7 年前

    我和2有一个线性布局 TextView s水平对齐。第一个必须在屏幕中左对齐,第二个必须在屏幕中右对齐(它们之间存在空格)。我希望文本填充屏幕,使其占用尽可能少的空间。此外,如果文本太短,它应该占据整个屏幕线,而不是一半。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            tools:text="Example of code" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            tools:text="Sample text here, it shouldn't occupy much space" />
    
    </LinearLayout>
    

    如何在XML中执行此操作?

    使现代化

    第一和第二 文本框 s可以是不同的长度。

    此时将显示布局,以便: enter image description here .

    我希望这样: so .

    更新2

    一个比一开始更好的布局。这不是我想要的全部,但解决了一个问题。

    <?xml version="1.0" encoding="utf-8"?>
    <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="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            tools:text="Example of code" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:text="Sample text here, it shouldn't occupy much space" />
    
    </LinearLayout>
    
    5 回复  |  直到 7 年前
        1
  •  1
  •   Uthaya Waza_Be    7 年前

    嗨,你能检查一下这个吗?如果它不只是给我你想要的格式,我会帮你的。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
    <TextView
        android:id="@+id/nice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:layout_alignParentLeft="true"
        tools:text="Awesome" />
    
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/nice"
        android:textAlignment="textEnd"
        android:layout_alignParentRight="true"
        android:background="@color/colorAccent"
        tools:text="Welcome"
        android:gravity="end" />
    </RelativeLayout>
    

    非常感谢。

        2
  •  0
  •   JBA    7 年前

    如果您将此添加到两个TextView标记中,它会满足您的需要吗?

    android:layout_weight="1"
    
        3
  •  0
  •   tanni tanna    7 年前
    <?xml version="1.0" encoding="utf-8"?>
    <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="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_weight="1"
            android:id="@+id/text1"
            android:gravity="start" //left allign
            android:layout_width="0dp"
            android:layout_marginEnd="10dp" // for spacing between two
            android:layout_height="wrap_content"
            tools:text="Example of code" />
    
        <TextView
            android:gravity="end"   // right allign
            android:layout_weight="1"
            android:id="@+id/text2"
            android:layout_width="0dp"
            android:layout_marginStart="10dp" // for spacing between two
            android:layout_height="wrap_content"
            tools:text="Sample text here, it shouldn't occupy much space" />
    
    </LinearLayout>
    

    相等布局权重等分可用宽度,增加需要更多空间的TextView的权重。

        4
  •  0
  •   João Carlos    7 年前

    我不确定是否了解您的问题,但您可以在这些文本视图之间添加视图,或者使用 RelativeLayout 作为父级并设置 android:layout_alignParentRight="true" 在第二个文本子级上;

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@color/colorAccent"
            tools:text="Example of code" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:background="@color/colorAccent"
            android:gravity="center_horizontal"
            tools:text="long long long " />
    
    </LinearLayout>
    
        5
  •  0
  •   PeterOne    7 年前

    我不知道我是否理解正确。尝试右match\u parent和gravity“end”中的“文本视图”。

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginEnd="10dp"
            tools:text="Example of code"
            android:layout_marginRight="10dp" />
    
        <TextView
            android:gravity="end"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:text="Sample text here, it should not occupy much space" />
    
    
    </LinearLayout>