【问题标题】:Android. Align views inside linear layout without push away from screen.安卓。在线性布局内对齐视图,而不会从屏幕上推开。
【发布时间】:2016-12-08 08:11:24
【问题描述】:

我正在创建应用程序,其项目列表应如下所示:

|-----------------------------------------------------|
|TextView--View--View--View---------------------------|
|-----------------------------------------------------|

|-----------------------------------------------------|
|TextViewTextViewTextView--View--View--View-----------|
|-----------------------------------------------------|

|-----------------------------------------------------|
|TextViewTextViewTextViewTextView..--View--View--View-|
|-----------------------------------------------------|

但在我的情况下,第三行 TextView 推开三个视图:

|--------------------------------------------------|
|TextViewTextViewTextViewTextViewTextView--View--Vi|
|--------------------------------------------------|

我的 xml:

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

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

      <TextView
        android:id="@+id/TextViewSubject"
        android:text="SUBJECT"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


      <View
          android:background="#FF0000"
          android:layout_weight="0"
          android:layout_width="10dp"
          android:layout_height="10dp"/>
      <View
          android:layout_weight="0"
          android:background="#00FF00"
          android:layout_width="10dp"
          android:layout_height="10dp"/>
      <View
          android:layout_weight="0"
          android:background="#0000FF"
          android:layout_width="10dp"
          android:layout_height="10dp"/>

  </LinearLayout>

</LinearLayout>

我也试过使用RelativeLayout,但这样3个视图总是对齐右父级。我希望 3 个视图跟随 textview 而不是被推离屏幕。

这样可以吗?任何帮助将不胜感激。

【问题讨论】:

  • 如果设备上的空间不可用,那么它们怎么能全部适应。如果 textviews 覆盖了 75% 的屏幕,那么大于 25% 的视图怎么能适应。即使你设法适合此屏幕尺寸,但在较小的屏幕尺寸上,它仍可能溢出。
  • @AbhinavPuri 是的。我明白这一点。我希望这 3 个视图始终显示在屏幕上,并将 textview 留给这 3 个视图左侧的可用空间。如果没有足够的空间,那么 textview 可能是不可见的。

标签: android xml android-layout android-linearlayout


【解决方案1】:
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:text="largeTextlargeTextlargeTextlargeTextlargeTextlargeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:ellipsize="end"
        android:maxLines="1"
        android:paddingRight="95dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView"
        android:orientation="horizontal">
    <View
        android:background="@android:color/black"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/button3" />
    <View
        android:background="@android:color/black"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/button2" />
    <View
        android:background="@android:color/black"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:id="@+id/button" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    在第二个 linearLayout 中添加 android:weightSum=4 并在每个子视图中创建 android:layout_weight=1 并确保 android:layout_width 在子视图中设置为 0dp

    【讨论】:

    • 在这种情况下,布局中的所有视图都将具有相同的宽度。我需要 3 个固定宽度 (10dp) 的视图。
    • 如果您希望文本视图比视图大 5 倍,您可以将 weightSum 设置为 8,将 textView 权重设置为 5,每个视图为 1,但如果您真的想要 10dp 精确使用RelativeLayout 并将视图与 10dp 固定宽度对齐并设置填充左侧的 textView -> 对齐到左侧并设置android:layout_right="id of the view to the right"。希望这会有所帮助
    【解决方案3】:

    尝试在线性布局中使用权重

    这里,我已经修好了,你可以改变权重来增加按钮的大小

    <?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">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="30"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/TextViewSubject"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_weight="27"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="SUBJECT" />
    
    
        <View
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="10dp"
            android:background="#FF0000" />
    
        <View
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="10dp"
            android:background="#00FF00" />
    
        <View
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="10dp"
            android:background="#0000FF" />
    
    </LinearLayout>
    

    【讨论】:

    • 我使用了你的代码。 3 个视图与右侧对齐。我需要将它们对齐到 textview 的左侧。
    • 您将视图对齐到 Textview 的左侧。
    • 如果你想左对齐而不是将你的视图放在 Textview 上方,那么所有的视图都对齐到左侧
    • 我想我们并不了解对方。我想把第一个 textview 放在这个 textview 的右边 3 个视图。即使文本视图很长,屏幕上也应该始终显示 3 个视图。
    【解决方案4】:

    试试这个,希望这是你想要的。我刚刚删除了重量线。祝你好运

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:id="@+id/TextViewSubject"
                android:text="SUBJECT"
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_alignParentLeft="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
    
            <View
                android:background="#FF0000"
                android:layout_width="10dp"
                android:layout_height="10dp"/>
            <View
                android:background="#00FF00"
                android:layout_width="10dp"
                android:layout_height="10dp"/>
            <View
                android:background="#0000FF"
                android:layout_width="10dp"
                android:layout_height="10dp"/>
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 我也在尝试减轻重量,但这不适用于长文本视图。
    • 我们已经使用了`android:singleLine="true"`所以我们的文本视图不应该那么长。但如果您想阻止这一点,您可以使用 `android:maxLength="30" ` 限制文本长度。所以如果你有一个长文本,它只会显示 30 个字符。
    猜你喜欢
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多