【问题标题】:Android "Add a vertical line" on "RelativeLayout" / "Linear Layout"Android 在“RelativeLayout”/“线性布局”上“添加一条垂直线”
【发布时间】:2013-09-14 00:58:12
【问题描述】:

我已阅读有关在 Android 中添加垂直线的其他问题,这是我的问题

  1. 我能够在主 RelativeLayout 下添加一条垂直线作为“视图”,但无法在子 RelativeLayout 中添加。

垂直线的代码

<View
android:id="@+id/verticalSeperatorHours"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="@color/medium_dark_gray" 
/>

水平线的代码:

<LinearLayout
            android:id="@+id/currentTimeMarkerLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="100dp"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:padding="0dp" >

            <View
                android:layout_width="0dp"
                android:layout_height="3dp"
                android:layout_weight="1" />

            <View
                android:id="@+id/currentTimeLineView"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="14"
                android:background="@color/strong_blue" />
        </LinearLayout>

我的结构xml:

  • 相对布局
    • 线性布局
    • 滚动视图
      • 相对布局
        • 相对布局
        • “添加垂直线不起作用”
        • “添加水平线有效”
  • 线性布局
  • 在此处添加垂直线可以,但不是我想要的”

没有出现水平线LinearLayout后的“View”。我意识到我没有深入了解android布局的结构,所以有人可以向我解释它是如何工作的。

完整的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:arm="http://schemas.android.com/apk/res/edu.cmu.sv.arm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp" >

<LinearLayout ...


<View
    android:id="@+id/dividerView"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:layout_below="@+id/dayLabelsLinearLayout"
    android:background="@color/medium_gray" />

<ScrollView
    android:id="@+id/calendarScrollView"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/dividerView"
    android:layout_alignParentBottom="true"
    android:overScrollMode="never"
    android:padding="0dp"
    android:scrollbars="none"
    android:fadingEdge="none" >

    <RelativeLayout
        android:id="@+id/calendarRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="0dp" >

        <LinearLayout ...


        <LinearLayout
            android:id="@+id/currentTimeMarkerLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="100dp"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:padding="0dp" >

            <View
                android:layout_width="0dp"
                android:layout_height="3dp"
                android:layout_weight="1" />

            <View
                android:id="@+id/currentTimeLineView"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_weight="14"
                android:background="@color/strong_blue" />
        </LinearLayout>
        <View
                android:id="@+id/verticalSeperatorHours"
                android:layout_width="1dip"
                android:layout_height="match_parent"
                android:layout_marginLeft="100dp"
                android:background="@color/medium_dark_gray" />
    </RelativeLayout>
</ScrollView>



<LinearLayout ...

<View ...

</RelativeLayout>

【问题讨论】:

  • 嗯,这是一种显示布局的新方式。请编辑并显示您的完整布局 XML,而不仅仅是您的“结构”和“水平线”。

标签: android android-layout android-linearlayout android-relativelayout


【解决方案1】:

您应该在使用 View 后完成 LinearLayout。

我在我的 XML 文件中使用了以下代码,并且它有效。

它也应该适合你。

<LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="1dp"
     android:paddingLeft="5dip"
     android:paddingRight="5dip">
<View 
    android:id="@+id/view"
    android:layout_toRightOf="@+id/text1"
    android:layout_width="1dip"
    android:layout_height="fill_parent"
    android:background="#FF0000FF"/>
 </LinearLayout>

【讨论】:

  • 感谢您的回复。但这并不是我要问的。我的水平线和你的一样,但另一方面,类似的代码不适用于垂直线。
  • 我已经更新了我的答案,再试一次。它应该可以工作,我刚刚在我的布局中实现了这个。 @ChongYue
【解决方案2】:

我使用形状来设置垂直和水平线。

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:background="@drawable/drawable_profile_week_back"
        android:gravity="right"
        android:paddingLeft="1dip"
        android:paddingRight="1dip"
        android:orientation="vertical"
        android:minHeight="60dp"
        android:layout_height="60dp"   >



    </LinearLayout>

drawable => drawable_profile_week_back

sion="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="-10dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

    <item
        android:bottom="-10dp"
        android:left="60dp"
        android:right="-10dp"
        android:top="-10dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

    <item
        android:bottom="-10dp"
        android:left="60dp"
        android:right="-10dp"
        android:top="14dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

    <item
        android:bottom="-10dp"
        android:left="60dp"
        android:right="-10dp"
        android:top="29dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

    <item
        android:bottom="-10dp"
        android:left="60dp"
        android:right="-10dp"
        android:top="44dp">
        <shape android:shape="rectangle" >
            <stroke
                android:width="1dp"
                android:color="#a6a6a6" />
        </shape>
    </item>

</layer-list>

【讨论】:

    【解决方案3】:

    试试这个代码的垂直线

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_marginTop="100dp"
        android:background="#000000" />
    
    </LinearLayout>
    

    或

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_marginTop="100dp"
        android:background="#000000" />
    

    【讨论】:

    • 这是非常简单的代码,只给出高度和宽度
    猜你喜欢
    • 1970-01-01
    • 2017-08-26
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多