【问题标题】:android border around view not working视图周围的android边框不起作用
【发布时间】:2013-10-11 07:26:12
【问题描述】:

我在RelativeLayout 中得到以下结果,其中包含 5 个视图(上、下、左、右、中),然后将相机预览放入其中。

我还想要在中心视图周围画一个边框。我为它使用了一个可绘制的形状,但它没有用。边框适用于相对布局中除中心之外的所有视图。

如何在它周围画边框?

这是布局:

<?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" >

    <RelativeLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="1" >

        <View
            android:id="@+id/top"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_alignParentTop="true"
            android:layout_gravity="center"
            android:background="#80000000" />

        <View
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_alignParentBottom="true"
            android:layout_gravity="center"
            android:background="#80000000" />

        <View
            android:id="@+id/left"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_above="@id/bottom"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/top"
            android:layout_gravity="center"
            android:background="#80000000" />

        <View
            android:id="@+id/right"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_above="@id/bottom"
            android:layout_alignParentRight="true"
            android:layout_below="@id/top"
            android:layout_gravity="center"
            android:background="#80000000" />

        <View
            android:id="@+id/center"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/bottom"
            android:layout_below="@id/top"
            android:layout_gravity="center"
            android:layout_margin="25dp"
            android:layout_toLeftOf="@+id/left"
            android:layout_toRightOf="@id/right"
            android:background="@drawable/border_red" />

    </RelativeLayout>

    <Button
        android:id="@+id/button_capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Capture" />

</LinearLayout>

这里是border_red.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00000000"/>
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
    <stroke android:color="#FF0000" android:width="5dp" />
</shape>

【问题讨论】:

    标签: android border android-relativelayout shape


    【解决方案1】:

    这是因为您为View 留出了余量,这对于该区域来说太大了。这就是为什么您的中心View 不可见。将您的中心View 替换为下面的View代码。这对我有用,希望它也对你有用 例如。

    <View
            android:id="@+id/center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/bottom"
            android:layout_below="@id/top"
            android:layout_gravity="center"
            android:layout_toLeftOf="@+id/right"
            android:layout_toRightOf="@+id/left"
            android:background="@drawable/border_red" />
    

    【讨论】:

      【解决方案2】:

      将高度降低到5dp 像这样android:layout_height="5dp" in View

      水平线

        <View
           android:id="@+id/view"
           android:background="#FF4500"
           android:layout_width="fill_parent"
           android:layout_height="2dp"     
           android:layout_below="@+id/listview_id" />
      

      垂直线

       <View
         android:id="@+id/view"
         android:background="#FF4500"
         android:layout_width="2dp"
         android:layout_height="fill_parent"     />
      

      【讨论】:

      • 这甚至与我想要做的事情无关。
      【解决方案3】:

      另一种解决方案:-

            <LinearLayout
              android:id="@+id/center"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_above="@id/bottom"
              android:layout_below="@id/top"
              android:layout_gravity="center"
              android:layout_margin="25dp"
              android:layout_toLeftOf="@+id/left"
              android:layout_toRightOf="@id/right"
              android:background="#FF0000"
              android:padding="5dp">
      
              <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00000000"/>
            </LinearLayout>
      

      【讨论】:

      • 哦……!!当您将相机预览放置在View时,它肯定会起作用。即您必须在其中添加一个孩子View
      • 相机预览放在整个RelativeLayout上。中心视图是无用的。我添加它只是为了显示边框。
      • 我们可以把一个视图放在另一个视图中吗?
      猜你喜欢
      • 2021-12-29
      • 2015-06-19
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多