Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用样式和属性重用具有不同值的布局-Android_Android_Attributes_Styles - Fatal编程技术网

使用样式和属性重用具有不同值的布局-Android

使用样式和属性重用具有不同值的布局-Android,android,attributes,styles,Android,Attributes,Styles,我有一个布局边框\u bottom\u black\u bluegrey\u background.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="rectangle" >

我有一个布局边框\u bottom\u black\u bluegrey\u background.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="@color/black" />
        </shape>
    </item>
    <item android:bottom="1dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/blue_grey" />
        </shape>
    </item>

</layer-list>

我试图重用这个布局,这样我就不必为每个不同的边框和背景色创建一个

我知道这可以通过样式和属性来实现,我曾在互联网上寻找过好的教程,但没有用

到目前为止,我得到的是:

attrs.xml

<resources>
    <declare-styleable>
        <attr name="backgroundColor" format="reference"/>
        <attr name="borderColor" format="reference"/>
    </declare-styleable>
</resources>
 <style name="BorderBottom">
    <item name="android:background">@drawable/border_bottom</item>
</style>

<style name="BlackBorderBlueGreyBackground" parent="BorderBottom">
    <item name="borderColor">@color/black</item>
     <item name="backgroundColor">@color/blue_grey</item>
</style>
<?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="@color/black" />
            <solid android:color="?attr/borderColor" />

        </shape>
   </item>

   <item android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <solid android:color="?attr/backgroundColor" />
        </shape>
   </item>

</layer-list>

styles.xml

<resources>
    <declare-styleable>
        <attr name="backgroundColor" format="reference"/>
        <attr name="borderColor" format="reference"/>
    </declare-styleable>
</resources>
 <style name="BorderBottom">
    <item name="android:background">@drawable/border_bottom</item>
</style>

<style name="BlackBorderBlueGreyBackground" parent="BorderBottom">
    <item name="borderColor">@color/black</item>
     <item name="backgroundColor">@color/blue_grey</item>
</style>
<?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="@color/black" />
            <solid android:color="?attr/borderColor" />

        </shape>
   </item>

   <item android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <solid android:color="?attr/backgroundColor" />
        </shape>
   </item>

</layer-list>

@可拉深/边框/底部
@颜色/黑色
@颜色/蓝/灰
border\u bottom.xml

<resources>
    <declare-styleable>
        <attr name="backgroundColor" format="reference"/>
        <attr name="borderColor" format="reference"/>
    </declare-styleable>
</resources>
 <style name="BorderBottom">
    <item name="android:background">@drawable/border_bottom</item>
</style>

<style name="BlackBorderBlueGreyBackground" parent="BorderBottom">
    <item name="borderColor">@color/black</item>
     <item name="backgroundColor">@color/blue_grey</item>
</style>
<?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="@color/black" />
            <solid android:color="?attr/borderColor" />

        </shape>
   </item>

   <item android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <solid android:color="?attr/backgroundColor" />
        </shape>
   </item>

</layer-list>

我的风格实现

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/BlackBorderBlueGreyBackground"
    android:orientation="vertical" >

</RelativeLayout>


有人能给我指出正确的方向吗?

我认为问题在于,当您使用中使用的attrs.xml时。我在上面的代码中看到的问题是,border_bottom.xml指向尚未明确声明的颜色属性,它无法知道应该是什么颜色。如果你想创建一个布局,你可以继续使用我的建议是创建一个类,为你创建背景,如果需要的话,你可以只保存创建的可绘制的。这是一个我用来在视图下画下划线的类,我认为它应该足够了

public final class Background extends ShapeDrawable {
public final static int RECTANGLE = 0x00;
public final static int ROUND_RECTANGLE = 0x01;
public final static int UNDERLINE = 0x02;
    //For future use
//public final static int CIRCLE = 0x03;

private int shape_type;
private final Paint fill_paint, stroke_paint;
private int stroke_width;

private static RectShape rectangle = new RectShape();
private static RoundRectShape round_rectangle;
//private static OvalShape circle = new OvalShape();

public static final Background newInstance(int shape, int background_color, int stroke_color, int stroke_width) {
    switch (shape) {
    case UNDERLINE:
    case RECTANGLE:
        return new Background(shape, rectangle, background_color, stroke_color, stroke_width);
    case ROUND_RECTANGLE:
        round_rectangle = new RoundRectShape(new float[] { stroke_width, stroke_width,
                stroke_width, stroke_width, stroke_width, stroke_width, stroke_width, stroke_width }, null, null);

        return new Background(shape, round_rectangle, background_color, stroke_color, stroke_width);
    default:
        return null;
    }
}

private Background(int shape_type, Shape shape, int fill_color, int stroke_color, int stroke_width) {
    super(shape);
    this.shape_type = shape_type;
    this.stroke_width = stroke_width;

    //set the fill_paint
    fill_paint = new Paint(getPaint());
    fill_paint.setColor(fill_color);
    stroke_paint = new Paint(fill_paint);

    //set the stroke paint
    stroke_paint.setStyle(Paint.Style.STROKE);
    stroke_paint.setAntiAlias(false);
    stroke_paint.setStrokeWidth(stroke_width);
    stroke_paint.setColor(stroke_color);
}

@Override   
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
    switch(shape_type) {
    case UNDERLINE:
        final RectF underline = new RectF(getBounds().left - stroke_width, getBounds().top - stroke_width, 
                getBounds().right + stroke_width,
                getBounds().bottom - stroke_width);

        canvas.drawRect(underline, stroke_paint);
        break;
    case RECTANGLE:
        final RectF rectangle = new RectF(stroke_width, stroke_width, getBounds().right - stroke_width,
                getBounds().bottom - stroke_width);

        canvas.drawRect(rectangle, fill_paint);
        canvas.drawRect(rectangle, stroke_paint);
        break;
    }
}
}

然后,您只需保存创建的绘图表,并在代码中的多个布局上使用它

//untested code
Layout layout = (RelativeLayout)findViewById(R.id.your_layout_id);
Background background = Background.newInstance(Background.RECTANGLE, Color.GRAY, Color.BLUE, 1);
layout.setBackground(background);

1dp
可能是错误。尝试一个更大的值,比如5dp.1dp用于正确显示的边框,我尝试只使用一个布局,通过样式和属性指定背景颜色和边框颜色s@MikeBryant首先,我不知道在哪里可以将自定义属性应用到布局(为此,您必须实现自定义的RelativeLayout)。其次,我不明白您在
border\u bottom\u black\u bluegrey\u background.xml中声明的第一个可绘制文件的用途。您能解释一下吗?目前我必须为每个边框/背景颜色组合创建一个布局。这就是为什么我有一个“border\u bottom\u black\u bluegrey\u background.xml”我只希望有一个“border\u bottom.xml”,在这里我可以通过样式指定边框和背景颜色,我在border\u bottom.xml中指定属性(可能不正确)我有23个小时的时间来奖励奖金,但是我不知道如何在不开始新的奖金的情况下延长它。如果你能在那之前找到答案,奖金就归你了!