【问题标题】:What would the layout XML look like for this?布局 XML 会是什么样子?
【发布时间】:2011-09-08 20:08:12
【问题描述】:

这可能是一个非常愚蠢的问题,但我有几个关于这些列表项的布局 XML 的问题

我知道有三个文本视图,但它们是如何在最后一行留下缩进的?另外,它们是如何导致第二个 textview 在一定数量的行之后换行的?

如果有人可以发布示例 XML,那也太棒了。

【问题讨论】:

  • 它是一个自定义列表视图。最后一个文本使用重力属性设置为右。至于将 textview 限制为 x 行数,请使用 setMaxLines(x);

标签: android android-layout


【解决方案1】:

使用下面的xml,最后一个textview在右边。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:maxLines="2" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    <TextView 
        android:maxLines="5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    <TextView 
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />


</LinearLayout>

【讨论】:

    【解决方案2】:

    最后一个 TextView 要么布局向右对齐 (layout_gravity),要么与父宽度匹配并且其重力(不是布局,而是它自己的)向右。

    要椭圆化第二个 TextView,给它一个有效的高度,然后将 ellipsize 属性设置为 end。

    【讨论】:

      【解决方案3】:

      配方是 ScrollView + TextView + LinearLayout + 重力。 对于相同的外观,请检查:

      <?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="fill_parent"
          android:orientation="vertical">
      
      
      <ScrollView 
          android:id="@+id/sv"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"> 
      <LinearLayout 
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          >
          <TextView  
          android:layout_width="fill_parent" 
          android:textSize="24dip"
          android:layout_height="wrap_content" 
          android:text="Morning"
          />
          <TextView  
          android:layout_width="fill_parent" 
          android:textSize="14dip"
          android:layout_height="wrap_content" 
          android:text="Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through. "
          />
          <TextView  
          android:layout_width="fill_parent" 
          android:textSize="12dip"
          android:layout_height="wrap_content" 
          android:text="Morning"
          android:gravity="right"
          />
              <TextView  
          android:layout_width="fill_parent" 
          android:textSize="24dip"
          android:layout_height="wrap_content" 
          android:text="Morning"
          />
          <TextView  
          android:layout_width="fill_parent" 
          android:textSize="14dip"
          android:layout_height="wrap_content" 
          android:text="Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through. "
          />
          <TextView  
          android:layout_width="fill_parent" 
          android:textSize="12dip"
          android:layout_height="wrap_content" 
          android:text="Morning"
          android:gravity="right"
          />
          <TextView  
          android:layout_width="fill_parent" 
          android:textSize="24dip"
          android:layout_height="wrap_content" 
          android:text="Morning"
          />
          <TextView  
          android:layout_width="fill_parent" 
          android:textSize="14dip"
          android:layout_height="wrap_content" 
          android:text="Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through. "
          />
          <TextView  
          android:layout_width="fill_parent" 
          android:textSize="12dip"
          android:layout_height="wrap_content" 
          android:text="Morning"
          android:gravity="right"
          />
      </LinearLayout>
      </ScrollView>
      
      </LinearLayout>
      

      (您可以将TextViews 替换为ListView

      编辑:我想每个人都在这里给你一些解决方案:)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-11
        • 2017-03-18
        • 1970-01-01
        • 1970-01-01
        • 2013-02-28
        • 2011-09-09
        • 2010-09-15
        • 1970-01-01
        相关资源
        最近更新 更多