【问题标题】:Android Nested LinearLayout, Gravity not having expected resultsAndroid嵌套线性布局,重力没有预期的结果
【发布时间】:2013-12-25 00:22:09
【问题描述】:

嵌套 LinearLayouts 用作 ListView 中的自定义行时遇到问题。

App是一种企业内部电话簿,这一行会在ListView中重复出现。

当重力设置为左侧时,右侧的“Details Witheld”看起来不错,但如果重力设置为中心,则它显示在页面的一半之外。

我猜我的父 LinearLayout 对于屏幕来说太宽了,但它被设置为 fill_parent 所以我不知道怎么做。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"

完整的 XML 源代码如下。

左对齐看起来像这样,我希望 Details 是一个空格,因此它位于 Witheld 上方的中心。

对齐中心看起来像这样

完整的 XML 源代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal" >

  <!--  Left side name & post / parish -->
  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="80"
      android:orientation="vertical" >
    <TextView
        android:id="@+id/txtrowname"
        android:layout_marginBottom="2dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Smtih, Mr John"
        android:textSize="24sp"
        android:textColor="@color/darkblue_text"                
        android:textStyle="normal" />
    <TextView
        android:id="@+id/txtrowpost"
        android:layout_marginBottom="2dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="12 Some Street, SomeTown.  SS11 1SS"
        android:textSize="12sp"
        android:textColor="#000000"
        android:textStyle="normal" />
  </LinearLayout>
  <!--  Details withheld on right -->
  <TextView
      android:id="@+id/txtrowwitheld"
      android:layout_width="0dp"
      android:layout_height="fill_parent"
      android:text="Details Witheld"
      android:layout_weight="20"
      android:textSize="18sp"
      android:gravity="center"
      android:lines="2"
      android:maxLines="2"
      android:textColor="#FF0000"
      android:layout_marginBottom="2dp"
      android:textStyle="normal" />
</LinearLayout>

Ramesh 建议的带有 layout_gravity 的图像

【问题讨论】:

    标签: android xml android-layout android-listview android-linearlayout


    【解决方案1】:

    尝试使用 layout_gravity 代替重力,它会起作用并且不要使用 line="2"。如果宽度超过,它将自动适应 2 行。

    【讨论】:

    • 谢谢,请看我上面的编辑,看看现在的样子。不再存在离开页面但未居中的问题。
    【解决方案2】:

    我最终只是在没有居中的情况下凑合。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 2017-11-14
      • 2021-11-03
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      相关资源
      最近更新 更多