两种颜色背景Android

时间:2015-04-27 08:17:25

标签: android android-layout android-activity background background-color

我正在编写Android活动,它的背面应该有一个颜色,底部应该有另一种颜色。

使用的相对布局是填充的。

我通常使用非填充相对布局并将其分为上下相对布局,这些u / l分为其他填充相对布局。

这向我保证所有活动区域都具有应该具有的背景颜色。 填充区域确保小部件位于活动的中心附近。

但是现在我有一个已编程的活动,上面的小部件和底部的小部件相互关联,所以我不能轻易地划分相对布局。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

首先将此添加到您的[[u'something', u'a.b.c']] 。如果文件不存在,请将其创建为资源类型。

/values/attrs.xml

接下来创建一个<declare-styleable name="TwoColoredView"> <attr name="topColor" format="color"/> <attr name="bottomColor" format="color"/> <attr name="topColorHeightPercent" format="integer"/> </declare-styleable> 类,并将其放在保存自定义视图的位置

TwoColoredView

现在为片段/布局

创建布局
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import your.package.R;

/**
 * Created by Bojan on 27.4.2015.
 */

public class TwoColoredView extends View {

    private int measuredWidth, measuredHeight;
    private Paint topPaint, bottomPaint;
    final int defaultTopColor = 0xFFFF0000;
    final int defaultBottomColor = 0xFF0000FF;
    private int topHeight = 40;

    public TwoColoredView(Context context) {
        super(context);
        init(context, null, 0);
    }

    public TwoColoredView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public TwoColoredView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attributeSet, int style) {

        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.TwoColoredView, style, style);

        int topColor = typedArray.getColor(R.styleable.TwoColoredView_topColor, defaultTopColor);
        int bottomColor = typedArray.getColor(R.styleable.TwoColoredView_bottomColor, defaultBottomColor);
        topHeight = typedArray.getInteger(R.styleable.TwoColoredView_topColorHeightPercent, 40);

        typedArray.recycle();

        topPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        topPaint.setStyle(Paint.Style.FILL);
        topPaint.setColor(topColor);

        bottomPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bottomPaint.setStyle(Paint.Style.FILL);
        bottomPaint.setColor(bottomColor);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        measuredHeight = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        measuredWidth = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);

        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawRect(0, 0, measuredWidth, measuredHeight * 0.01f * topHeight, topPaint);
        canvas.drawRect(0, measuredHeight * 0.01f * topHeight, measuredWidth, measuredHeight, bottomPaint);
    }
}

这是最终结果

preview

答案 1 :(得分:2)

查看您的活动的此布局。只有一个RelativeLayout有两个背景。一个设置为布局本身,另一个设置为空View,位于顶部的视图下方。唯一的缺点是您必须从padding中删除RelativeLayout,并将其替换为设置为您的观看次数的margin。我想这不是什么大问题。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#59C2F2">

    <!-- This is the last TextView of top part, below it the bg will be different -->
    <TextView
        android:id="@+id/top_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="This represents top elements"/>

    <!-- This View must be placed before any elements from the bottom part -->
    <View
        android:id="@+id/bottom_background"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_text"
        android:layout_alignParentBottom="true"
        android:background="#004892" />

    <!-- Now you can place your bottom elements and align them with the to ones -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/bottom_background"
        android:layout_alignLeft="@+id/top_text"
        android:layout_marginTop="16dp"
        android:textColor="#fff"
        android:text="This is the bottom part"/>

</RelativeLayout>