(Android) Attrs & Style

The application package is "test.animation"


Attrs


/YOUR_APP/res/values/attrs.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="ButtonView">
        <attr name="hello_a" format="color" />
        <attr name="hello_b" format="dimension" />
    </declare-styleable>

</resources>


/YOUR_APP/res/layout/main.xml


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res/test.animation"

    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    ...

     <test.animation.ButtonView
            android:id="@+id/btnD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            custom:hello_a="@color/red"
            custom:hello_b="@dimen/wordsize"
            android:text="@string/btn_d" />

      ...

</ScrollView>


/YOUR_APP/src/test/animatioin/ButtonView

package test.animation;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.Button;

public class ButtonView extends Button {

    private Paint myPaint;

    public ButtonView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ButtonView(Context context) {
        super(context);
    }

    public ButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        myPaint = new Paint();
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.ButtonView);
        float textSize = a.getDimension(R.styleable.ButtonView_hello_b, 30);
        int textColor = a.getColor(R.styleable.ButtonView_hello_a, 0xFFFFFFFF);
        myPaint.setColor(textColor);
        myPaint.setTextSize(textSize);
        a.recycle();
    }
    
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
         //TODO: Apply myPaint to draw the view.
    }
}


Style


/YOUR_APP/res/values/styles.xml


<resources xmlns:android="http://schemas.android.com/apk/res/android">

    ...
    <style name="test_style">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
</resources>


/YOUR_APP/res/layout/main.xml


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:custom="http://schemas.android.com/apk/res/test.animation"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    ...

     <Button
            android:id="@+id/btnC"
            style="@style/test_style"
            android:text="@string/btn_c" />

      ...

</ScrollView>

 

 

<?xml version="1.0" encoding="utf-8"?><resources>

<style name="CodeFont" parent="@android:style/TextAppearance.Medium">

<item name="android:layout_width">fill_parent</item> 

<item name="android:layout_height">wrap_content</item>

<item name="android:textColor">#00FF00</item>

<item name="android:typeface">monospace</item>

</style></resources>


themes.xml

        <item name="windowBackground">@android:drawable/screen_background_dark</item>
        <item name="windowFrame">@null</item>
        <item name="windowNoTitle">false</item>
        <item name="windowFullscreen">false</item>
        <item name="windowIsFloating">false</item>
        <item name="windowContentOverlay">@android:drawable/title_bar_shadow</item>
        <item name="windowTitleStyle">@android:style/WindowTitle</item>
        <item name="windowTitleSize">25dip</item>
        <item name="windowTitleBackgroundStyle">@android:style/WindowTitleBackground</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Activity</item>

 

 

setTheme(android.R.style.Theme_Light);
setContentView(R.layout.linear_layout_3);

 

setTheme should be in the front of setContentView.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值