【问题标题】:Android: Putting a vertical divider/separator line between textviews in a horizontal linear layout nested in vertical linear layout?Android:在嵌套在垂直线性布局中的水平线性布局中的文本视图之间放置垂直分隔线/分隔线?
【发布时间】:2012-10-10 10:23:31
【问题描述】:

对于 android 内容视图,我有一个垂直线性布局,其中包含一些文本视图,这些文本视图有一些线来划分和分隔垂直元素,这很好用,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="fill_parent"
    android:orientation="vertical" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/A" />                 
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/B" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/C" />    
    </LinearLayout>    
    <View 
         android:background="#ffffff" 
         android:layout_width = "fill_parent"
         android:layout_height="1dip"/>    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/D" />
    <View  
         android:background="#ffffff" 
         android:layout_width = "fill_parent"
         android:layout_height="1dip" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/E" />
    </LinearLayout>

现在我想在嵌套文本视图中水平放置的文本视图之间添加一条垂直分隔线,其中包含字符串 A/B/C。当我尝试通过添加硬编码宽度视图来做到这一点时,该线跨越了父线性布局的整个高度。

    <?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" >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/A" />         
        <!--the vertical line separator-->
        <View  
     android:background="#ffffff" 
     android:layout_width = "1dip"
     android:layout_height="fill_parent" />         
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/B" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="@string/C" />    
    </LinearLayout>    
    <View 
         android:background="#ffffff" 
         android:layout_width = "fill_parent"
         android:layout_height="1dip"/>    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/D" />
    <View  
         android:background="#ffffff" 
         android:layout_width = "fill_parent"
         android:layout_height="1dip" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/E" />
    </LinearLayout>

对于这个垂直分隔符视图,我尝试改用android:layout_height="wrap_content"/&gt;,但显示的结果相同。

有没有办法在此处设置垂直分隔符,通过引入垂直线来分隔文本视图来保留高度?还是我必须选择不同的布局?

【问题讨论】:

  • 为什么不使用“match_parent”作为高度属性?

标签: android android-layout android-linearlayout android-view textview


【解决方案1】:

另一种选择可能是包含android:layout_margin(或单独的layout_marginToplayout_marginBottom)以在绘制的垂直线的顶部和底部与LinearLayout.的相应水平边缘之间创建一个空间

除此之外,我可能会尝试使用RelativeLayout, 并调整您的垂直线视图以使其顶部和底部与相邻的TextViews. 之一对齐

【讨论】:

    【解决方案2】:

    您必须为 LinearLayoutVertical Seperator

    提供固定高度

    【讨论】:

    • 是的,这在某种程度上是一种解决方案,但它不适用于更改文本大小。我希望有一个更优雅的解决方案。我放了 20dip,是的,这行得通。但我担心硬编码的高度可能会在以后引起问题。
    猜你喜欢
    • 2015-07-27
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多