【问题标题】:Layout two text views布局两个文本视图
【发布时间】:2012-12-13 05:09:43
【问题描述】:

我必须将两个文本视图放在一行中。第二个应该放在第一个旁边(如图 1 所示)。

我的问题是:第一个文本视图的文本可能很长,所以我需要省略它。它应该占用尽可能多的空间,以便在第二个文本视图中阅读整个文本。图 2 显示了它。

是否可以使用 Android 内置层来实现,或者我需要编写自定义视图?

【问题讨论】:

    标签: android layout


    【解决方案1】:

    创建一个内部布局并使用android:weightSum="100" 属性。然后在这个内部布局中放置两个文本视图,并为每个文本视图添加属性android:layout_weight="50"

    【讨论】:

      【解决方案2】:

      试试这样的

      <?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="horizontal" >
      
      <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:ellipsize="end"
          android:text="A long long text" />
      
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="A long text" />
      
      </LinearLayout>
      

      问候

      【讨论】:

      • 你好。它几乎按预期工作(我使用相对布局获得了相同的结果)。当第一个文本视图字符串很短(例如“test”)时,第二个文本视图放置在其父视图的右侧(这里:线性布局)。但在这种情况下,我希望它们都向左对齐。
      猜你喜欢
      • 1970-01-01
      • 2016-08-08
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      相关资源
      最近更新 更多