【问题标题】:android sdk vertically left align text in vertical linearlayoutsandroid sdk垂直左对齐垂直线性布局中的文本
【发布时间】:2015-08-25 06:20:08
【问题描述】:

我有一个包含 3 行文本/图像的远程视图,每行包含 2 个文本视图和一个图像视图。

我的结构是:-

垂直线性布局 水平线性布局 水平线性布局 水平线性布局。

我使用的布局xml是:-

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="5dp"
    android:paddingTop="5dp"
    android:paddingBottom="10dp"
    android:id="@+id/wevents"
    android:gravity="center_vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|left"
        android:layout_gravity="center_vertical"
        android:id="@+id/sub3">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="+10"
            android:id="@+id/wdays3"
            android:layout_weight="1"
            android:textColor="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Some Text"
            android:id="@+id/wsubject3"
            android:layout_weight="4"
            android:textColor="#ffffff"
            android:paddingLeft="5dp"
            android:scrollHorizontally="true"
            android:ellipsize="end"
            android:maxLines="1"
            android:singleLine="true"
            android:layout_gravity="left" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/walarm3"
            android:src="@drawable/bell_white"
            android:layout_weight="1"
            android:layout_gravity="center" />
    </LinearLayout>

    ... (each of the other linearlayouts is exactly the same)

我有两个问题:-

  1. 我无法让第二个文本字段在其前身下方垂直对齐。
  2. 如果第二个文本字段太长,它会将图像推到第二行。

所以:

  1. 如何使所有文本左对齐。
  2. 如何限制用省略号截断长文本以使图像也对齐?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    这里1和2的答案应该是一样的;您需要在 TextViews 和 ImageViews 上将 layout_width 属性更改为 0dp。 所以第一个 TextView 应该显示:

            <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="+10"
            android:id="@+id/wdays3"
            android:layout_weight="1"
            android:textColor="#ffffff" />
    

    然后对其他 TextViews 和 ImageViews 执行相同的操作。

    问题是当你在水平LinearLayout中为这些View使用layout_weight时,你是说每个view的宽度应该参考你指定的权重来计算,但是当你说layout_width="wrap_content",你是说宽度应该根据视图内部的内容来计算,即文本的长度或图像的大小。指定这两种方法来计算宽度会导致一些奇怪的结果,但如果你将宽度设置为 0dp,那么它将直接进入你设置的权重。

    这可能不言而喻,但我还是要说,如果您在垂直线性布局中为项目使用权重,则需要将 layout_height 属性设置为 0dp。

    【讨论】:

    • 完美,如梦似幻。另外,非常感谢它为什么像梦一样工作。这是我第一次看到如此简洁的布局权重、布局宽度解释。非常感谢。
    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多