硬编码带边框的矩形
问题描述:
我想绘制一个灰色的矩形,其周围只有一个红色的边框,但是我已经看到这种方式有许多不同的方式,所以我希望有人告诉我为了达到这个目的,哪种方法最好?以下是我的代码,但我想知道是否有更简单的方法。没有Java请。我只想要xml。硬编码带边框的矩形
<RelativeLayout
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@color/red"
android:layout_weight=".25">
<RelativeLayout
android:layout_width="7dp"
android:layout_height="7dp"
android:background="@color/grey"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
布局不对齐
<RelativeLayout
android:layout_width="0dp"
android:layout_height="40dp"
android:background="@color/grey"
android:layout_weight=".25" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="3"
android:textColor="@color/black"/>
<RelativeLayout
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@color/red"
android:layout_weight=".25">
<RelativeLayout
android:layout_width="7dp"
android:layout_height="7dp"
android:background="@color/grey"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="15dp"
android:layout_height="10dp"
android:background="@color/red"
android:layout_alignParentLeft="true">
<RelativeLayout
android:layout_width="12dp"
android:layout_height="7dp"
android:background="@color/grey"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="15dp"
android:layout_height="10dp"
android:background="@color/red"
android:layout_alignParentRight="true">
<RelativeLayout
android:layout_width="12dp"
android:layout_height="7dp"
android:background="@color/grey"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="15dp"
android:layout_height="10dp"
android:background="@color/red"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true">
<RelativeLayout
android:layout_width="12dp"
android:layout_height="7dp"
android:background="@color/grey"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="15dp"
android:layout_height="10dp"
android:background="@color/red"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:padding="1.5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="15dp"
android:layout_height="10dp"
android:background="@color/red"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
<RelativeLayout
android:layout_width="12dp"
android:layout_height="7dp"
android:background="@color/grey"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
</RelativeLayout>
答
在你绘制 rectangle.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#D7D5D6" >
</solid>
<stroke
android:width="0.5dp"
android:color="#ff0000" >
</stroke>
</shape>
而你的布局。
<?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:orientation="vertical"
android:background="@drawable/rectangle"
>
</LinearLayout>
您可以更改所需的颜色代码和边框宽度。
答
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#f4f4f4"
>
</RelativeLayout>
答
LinearLayout和RelativeLayout是视图组。相反,他们使用简单的视图
android:background =“@ drawable/rectangle” attribute(see next answer)。
+0
请不要参考下一个答案(这依赖于排序),而是复制粘贴必要的代码。 – tacone 2014-10-11 20:28:35
好的,你知道我怎样才能将我的亲戚从左侧而不是中间水平对齐30%?请检查我的代码上面的标题**布局不对齐** – MacaronLover 2014-10-11 22:04:58
我认为这似乎很大纲外....因为没有%的单位...和相对布局不固定......我认为这看起来不可能使用XML ....但通过Java代码,我们可以做到这一点... – 2014-10-11 22:11:51