模拟考试

需求一:使用Android Studio建项目,并通过github进行版本控制。(25分)

1) 使用studio创建一个项目github上创建“姓名全拼Project”仓库(5)

  2)使用git命令将项目上传至“姓名全拼Project”仓库(10分)

3) 使用studio clone到本地(5分),完成以下需求后将修改推到远程仓库(5分)

需求二:实现类似效果图TopBar复合型控件效果,如图165分)

1) 自定义控件样式实现没问题。(10分)

2) 支持自定义属性,在attrs.xml中配置正确:中间标题内容文字,字体大小,文字颜色,背景颜色(8分)

3) 支持自定义属性,在attrs.xml中配置正确:左边按钮文字,字体大小,文字颜色,背景颜色(8分)

4) 支持自定义属性,在attrs.xml中配置正确:右边按钮文字,字体大小,文字颜色,背景颜色(8分)

5) 构造方法中自定义属性配置正确(5分)

6) 左右按钮通过回调接口定义点击事件。(10分)

7) 点击左右按钮弹出吐司,如图1。(5分)

8) 在布局文件中使用复合型控件时使用自定义的属性赋值(5分)

9) 左右按钮背景设置背景选择器,点击时红色,未点击时蓝色。(6分)

/***************************************************************************************************************

MainActivity 
/

package com.example.weekonecustomview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


/******************************************************************
TopBar 
/
package com.example.weekonecustomview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by hasee on 2017/9/2.
 */

public class TopBar extends LinearLayout implements View.OnClickListener {

    private TextView title;
    private Button leftBtn;
    private Button rightBtn;

    public TopBar(Context context) {
        super(context);
        initView(context, null);
    }

    public TopBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View inflate = inflate(context, R.layout.top_bar_layout, this);
        title = (TextView) inflate.findViewById(R.id.title_tv);
        leftBtn = (Button) inflate.findViewById(R.id.left_btn);
        rightBtn = (Button) inflate.findViewById(R.id.right_btn);
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        if (attrs == null) {
            return;
        }
        initAttrs(context, attrs);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TopBar);

        title.setText(typedArray.getString(R.styleable.TopBar_title_text));
        leftBtn.setText(typedArray.getString(R.styleable.TopBar_left_btn_text));
        rightBtn.setText(typedArray.getString(R.styleable.TopBar_right_btn_text));

        title.setTextColor(typedArray.getColor(R.styleable.TopBar_title_text_color, Color.BLACK));
        leftBtn.setTextColor(typedArray.getColor(R.styleable.TopBar_left_btn_text_color, Color.BLACK));
        rightBtn.setTextColor(typedArray.getColor(R.styleable.TopBar_right_btn_text_color, Color.BLACK));

        title.setTextSize(typedArray.getDimension(R.styleable.TopBar_title_text_size, 16));
        leftBtn.setTextSize(typedArray.getDimension(R.styleable.TopBar_left_btn_text_size, 16));
        rightBtn.setTextSize(typedArray.getDimension(R.styleable.TopBar_right_btn_text_size, 16));

        title.setBackgroundColor(typedArray.getColor(R.styleable.TopBar_title_bg_color, Color.BLUE));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            leftBtn.setBackground(typedArray.getDrawable(R.styleable.TopBar_left_btn_bg_color));
            rightBtn.setBackground(typedArray.getDrawable(R.styleable.TopBar_right_btn_bg_color));
        }else{
            leftBtn.setBackgroundDrawable(typedArray.getDrawable(R.styleable.TopBar_left_btn_bg_color));
            rightBtn.setBackgroundDrawable(typedArray.getDrawable(R.styleable.TopBar_right_btn_bg_color));
        }
    }

    @Override
    public void onClick(View v) {
        String content ="";
        if (v.equals(leftBtn)) {
            content ="点击左侧按钮";
        } else if (v.equals(rightBtn)) {
            content ="点击右侧按钮";
        }
        Toast.makeText(v.getContext(),content,Toast.LENGTH_LONG).show();
    }
}

 /***************************************************************************************res/color/my_color_selected_dd.xml/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary"   android:state_pressed="false"/>
    <item android:color="@color/colorAccent" android:state_pressed="true" />
</selector>
/*************************************************************************************drawable/my_color_selected.xml/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/colorPrimary"   android:state_pressed="false"/>
    <item android:drawable="@color/colorAccent" android:state_pressed="true" />
</selector>
/**********************************************************************************lyout/activity_main.mal/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <com.example.weekonecustomview.TopBar
        app:title_text="这是我的activity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:left_btn_bg_color="@drawable/my_clolor_selected"
        app:left_btn_text="左侧"
        app:left_btn_text_color="#000000"
        app:left_btn_text_size="12dp"
        app:right_btn_bg_color="@drawable/my_clolor_selected"
        app:right_btn_text="右侧"
        app:right_btn_text_color="#000000"
        app:right_btn_text_size="12dp"
        app:title_bg_color="@color/colorAccent"
        app:title_text_color="#000000"
        />
    <Button
        android:textColor="@color/my_clolor_selected_dd"
        android:background="@drawable/my_clolor_selected"
        android:text="处处存储错错"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
/********************************************************layout/top_bar_layout.mal/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/left_btn"
        android:layout_width="80dp"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/title_tv"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="标题" />

    <Button
        android:id="@+id/right_btn"
        android:layout_width="80dp"
        android:layout_height="match_parent" />
</LinearLayout>
/****************************************************************8values/attrs.mal/
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TopBar">
        <attr name="title_text" format="string"></attr>
        <attr name="title_text_size" format="dimension"></attr>
        <attr name="title_text_color" format="color"></attr>
        <attr name="title_bg_color" format="color"></attr>

        <attr name="left_btn_text" format="string"></attr>
        <attr name="left_btn_text_size" format="dimension"></attr>
        <attr name="left_btn_text_color" format="color"></attr>
        <attr name="left_btn_bg_color" format="reference"></attr>

        <attr name="right_btn_text" format="string"></attr>
        <attr name="right_btn_text_size" format="dimension"></attr>
        <attr name="right_btn_text_color" format="color"></attr>
        <attr name="right_btn_bg_color" format="reference"></attr>
    </declare-styleable>
</resources>







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值