安卓学习笔记_20180327

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30241709/article/details/79717631

资源的使用:

Java代码中使用:

文字:

txtName.setText(getResource().getText(R.string.name));
图片:

imgIcon.setBackgroundDrawableResource(R.drawable.icon);

颜色:

txtName.setTextColor(getResources().getColor(R.color.red));

布局:

setCotentView(R.layout.main);
控件:

txtName = (TextView)findViewById(R.id.txt_name);

Android中的六大布局:

LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局),FrameLayout(表格布局), 

FrameLayout(帧布局), AbsuluteLayout(绝对布局),GridLayout(网格布局)

为LinearLayout设置分割线:

①直接在布局中添加一个view

②使用Layout的divider属性

布局还是建议使用RelativeLayout + LinearLayout的weight属性

margin与padding的区别:

margin是针对容器中的组件,padding是针对组件中的元素

TableLayout三个常用的属性:

android:collapseColumms: 设置需要被隐藏的列的序号

android:shrinkColums: 设置允许被收缩的列的序列号

android: stretchColumns: 设置允许被拉伸的列的序列号

FrameLayout常用属性:

android: foreground: 设置帧布局容器的前景图像

android: foregroundGravity: 设置设置前景图像显示的位置

GridLayout(网格布局)

几个单位:

dp(dip): device independent pixels(设备独立像素)

ps: pixel,像素

pt: point,标准长度单位

sp: scaled pixels(放大像素)

文本和超链接标签

<font color='blue'><b>百度一下,你就知道</b></font><br>

<a href = 'http://www.baidu.com'>百度</a>

SpannableString和SpannableStringBuilder(前者针对的是不可变文本,后者针对的是可变文本)


实现显示设备电量:

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            tv = findViewById(R.id.tv1);
            if(Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())){
                int level = intent.getIntExtra("level",0);
                int scale = intent.getIntExtra("scale",100);
                tv.setText("电池电量:" + (level*100/scale)+"%");
            }
        }
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态注册
        registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消注册
        unregisterReceiver(receiver);
    }
}

FrameLayout中TextView实现流水灯效果:

public class MainActivity extends AppCompatActivity implements Runnable{
    private int[] colors = {Color.RED, Color.MAGENTA, Color.GREEN, Color.BLUE, Color.CYAN};
    private int[] nextColorIndexs = {1, 2, 3, 4, 0};
    private View[] views;
    private Handler handler;
    private int currentColorIndex = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        views = new View[] {
                findViewById(R.id.tv1),
                findViewById(R.id.tv2),
                findViewById(R.id.tv3),
                findViewById(R.id.tv4),
                findViewById(R.id.tv5)
        };
        handler = new Handler();
        handler.postDelayed(this, 1000);
    }

    public void run(){
        int nextColorIndex = currentColorIndex;
        for(int i = 0; i<views.length; i++){
            views[i].setBackgroundColor(colors[nextColorIndexs[nextColorIndex]]);
            nextColorIndex = nextColorIndexs[nextColorIndex];
        }
        currentColorIndex++;
        if (currentColorIndex == 5){
            currentColorIndex = 0;
        }
        handler.postDelayed(this,1000);
    }
}

RatingBar: 评星控件

android:numStars: 指定评星数量

android:rating: 指定当前分数

android: stepSize: 指定分数的增量单位

public class MainActivity extends AppCompatActivity implements RatingBar.OnRatingBarChangeListener{
    RatingBar ratingBar;
    TextView textView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ratingBar = findViewById(R.id.rb);
        textView = findViewById(R.id.tv1);
        ratingBar.setOnRatingBarChangeListener(this);
    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        rating = ratingBar.getRating();
        textView.setText("分数为:"+ rating);
    }
}


猜你喜欢

转载自blog.csdn.net/qq_30241709/article/details/79717631