【问题标题】:Drawing 2 rectangle with cut inbetween绘制2个矩形,中间有切口
【发布时间】:2018-03-03 04:32:25
【问题描述】:

我想用给定的例子在xml中绘制like this.

我试过this solution.

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"><item android:right="100dp">
<shape android:shape="rectangle">
    <size android:height="100dp" android:width="100dp"/>
    <solid android:color="@android:color/black"/>
</shape></item><item android:left="100dp"><shape android:shape="rectangle">
    <size android:height="100dp" android:width="100dp"/>
    <solid android:color="@android:color/holo_green_light"/>
</shape></item></layer-list>

但它在 2 个形状之间没有中间切口。给定的形状也应该适用于任何宽度。所以不想定义任何宽度限制

result1

【问题讨论】:

  • 是按钮样式吗?
  • @droid 编辑了我的代码,现在检查一下

标签: android xml


【解决方案1】:

试试这个...

使用这种风格

<item >
    <shape android:shape="rectangle">
        <solid android:color="#FFCD80"/>
        <corners android:radius="5dp"/>
    </shape>
</item>

<item>
    <rotate android:fromDegrees="45" android:toDegrees="0" android:pivotX="20%" android:pivotY="10%" >
        <shape android:shape="rectangle" >
            <solid android:color="#FF0000" />
            <corners android:radius="5dp"/>
        </shape>
    </rotate>
</item>

布局

    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        tools:context=".game.GameActionActivity" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_style_twocolor_center_crossline" />

</RelativeLayout>

【讨论】:

  • 不,它不会给出相同的结果。左下角显示金色
  • 红色形状未占据全宽。形状的前半部分应覆盖至少 45% 的宽度
  • 我不知道您要为哪个组件使用这种样式。我将它用于具有静态高度和宽度的按钮。查看我的更新答案。
  • 两者都是在 relativeLayout 上包装内容
  • 仍然给出 3 种类型的部分 1 小金 2 是红色,3 又是金
【解决方案2】:

随心所欲地改变颜色。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportHeight="24.0"
    android:viewportWidth="24.0">

<path
    android:fillColor="#fff8bb"
    android:pathData="M0,0
                  L0,24
                  L24,24
                  L24,0 z" />
<path
    android:fillColor="#ffc5bb"
    android:pathData="M0,14
                  L8,24
                  L22,24
                  L8,0
                  L0,0z" />
</vector>

在xml文件中

<Button
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:background="@drawable/cust_rect"/>

【讨论】:

    猜你喜欢
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多