【问题标题】:how to set border linear layout on top and bottom如何在顶部和底部设置边框线性布局
【发布时间】:2015-01-19 15:35:31
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/home"
            android:layout_width="match_parent"
            android:layout_height="60dip"
            android:layout_marginRight="5dip"
            android:src="@anim/linearsavelocationbackground"
            android:weightSum="1" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="30dip"
                android:layout_height="30dip"
                android:layout_marginTop="15dip"
                android:layout_weight=".2"
                android:src="@drawable/homestatus" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="17dp"
                android:layout_weight=".6"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:text="Home"
                android:textColor="#3a3838"
                android:textSize="15dp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/homeimage"
                android:layout_width="30dip"
                android:layout_height="30dip"
                android:layout_marginRight="5dip"
                android:layout_marginTop="15dp"
                android:layout_weight="0.20"
                android:src="@drawable/deletingright" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/office"
            android:layout_width="match_parent"
            android:layout_height="60dip"
            android:layout_marginRight="5dip"
            android:background="#ffffff"
            android:src="@anim/linearsavelocationbackground"
            android:weightSum="1" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="30dip"
                android:layout_height="30dip"
                android:layout_marginTop="15dip"
                android:layout_weight=".2"
                android:src="@drawable/work" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="17dp"
                android:layout_weight=".6"
                android:gravity="center_vertical"
                android:paddingLeft="10dp"
                android:text="Home"
                android:textColor="#3a3838"
                android:textSize="15dp"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/homeimage"
                android:layout_width="30dip"
                android:layout_height="30dip"
                android:layout_marginRight="5dip"
                android:layout_marginTop="15dp"
                android:layout_weight="0.20"
                android:src="@drawable/deletingright" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

边框的背景

<?xml version="1.0" encoding="UTF-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <corners android:radius="20dp"/> 
   <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
   <solid android:color="#CCCCCC"/>
 </shape>

这是我的代码,我试图在 Linarlayout 上显示边框顶部和底部,但我无法显示效果我必须设置家庭线性布局和办公室线性布局的边框顶部和底部我试图设置但效果是不来请检查哪里出错了。

【问题讨论】:

  • 为什么要在 anim 文件夹中设置这个?它必须是可绘制文件夹中的可绘制对象...
  • 检查this

标签: android


【解决方案1】:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:bottom="1dp"
    android:left="-2dp"
    android:right="-2dp"
    android:top="1dp">
    <shape android:shape="rectangle" >
        <stroke
            android:width="1dp"
            android:color="@android:color/darker_gray" />

        <solid android:color="@android:color/transparent" />

        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />
    </shape>
</item>

使用上述布局作为背景边框。它会给你一个非常好的效果。如果您愿意,可以更改颜色和填充。将此作为 xml 添加到您的可绘制文件夹中,并将此可绘制作为布局的背景。

希望这会有所帮助。

【讨论】:

  • 效果不出来我叫同样的东西
  • 你是如何使用这个的?
  • 文件意外结束
【解决方案2】:

这可以使用 LayerList 来完成

创建可绘制名称border_top_bottom.xml

<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/your_border_color" />
    </shape>
</item>
<item
    android:top="1dp"
    android:bottom="1dp">
    <shape android:shape="rectangle" >
        <solid android:color="@color/your_fill_color" />
    </shape>
</item>

关于LinearLayout背景属性的使用

andorid:background:"@drawable/border_top_bottom.xml"

【讨论】:

    【解决方案3】:

    使用以下代码在您的可绘制对象中添加一个 xml,然后将此 xml 分配为您的线性布局背景

    <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
            <item>
              <shape 
                android:shape="rectangle">
                    <stroke android:width="1dp" android:color="#FF000000" />
                    <solid android:color="#FFDDDDDD" />
                </shape>
           </item>
           <item android:top="1dp" android:bottom="1dp"> 
              <shape 
                android:shape="rectangle">
                    <stroke android:width="1dp" android:color="#000" />
                    <solid android:color="#FFFFFF" />
                </shape>
           </item>
        </layer-list>
    

    【讨论】:

      【解决方案4】:

      我认为你的问题不是你的形状,而是你在布局中引用它的方式:

               <LinearLayout
              android:id="@+id/home"
              android:layout_width="match_parent"
              android:layout_height="60dip"
              android:layout_marginRight="5dip"
              android:src="@anim/linearsavelocationbackground"
              android:weightSum="1" >
      

      您已经设置了对动画文件夹的引用,但它必须在可绘制文件夹中。如果您的形状在 anim 文件夹内,请将其放入 drawable 文件夹并更正您的 LinearLayouts src 属性:

             android:src="@drawable/linearsavelocationbackground"
      

      如果这不是您想要的,请使用 krunal 和 EagleEye 中的示例创建一个形状。

      【讨论】:

      【解决方案5】:

      @Edge 请使用此代码 假设xml文件linearbg.xml的名称并将其放在资源下的drawable文件夹中。

       <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      
          <item>
              <shape>
                  <solid android:color="#EEEEEE" />
              </shape>
          </item>
      
          <!-- This is the line -->
          <item
              android:top="1dp"
              android:bottom="1dp">
              <shape>
                  <solid android:color="#FFFFFF" />
              </shape>
          </item>
      
      </layer-list>
      

      现在在你的线性布局中使用这个 linearbg.xml 作为背景 android:background="@drawable/linearbg"。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多