自定义View+Tablayout + ViewPager + Fragment的使用+AIDL的简单使用+NDK + Webview的使用

自定义View

CustomView

package bw.com.bw_goover_01.demo03;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * View中的具体内容是一个同心圆中包含一段红色的文字“已经选中商品!
 * Created by Administrator on 2018/3/2.
 */
public class CustomView extends View {
    //TODO 构造方法
    public CustomView(Context context) {
        super(context);
    }
    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    //TODO 绘制方法
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画笔
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);//设置画笔的颜色
        paint.setStyle(Paint.Style.STROKE);//设置画笔的样式, 空心
        //绘制圆
        canvas.drawCircle(500,500,300,paint);//中心的X, 中心的Y, 半径,画笔
        canvas.drawCircle(500,500,400,paint);
        //绘制文字
        paint.setStyle(Paint.Style.FILL);//设置画笔的样式, 实心
        paint.setColor(Color.RED);//设置画笔的颜色
        paint.setTextSize(40);//设置绘制文字的大小
        canvas.drawText("已经选中商品!",400,500,paint);
    }
}

 Main3Activity

 

package bw.com.bw_goover_01.demo03;

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

import bw.com.bw_goover_01.R;

public class Main3Activity extends AppCompatActivity {

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

    布局

  

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bw.com.bw_goover_01.demo03.Main3Activity">

    <bw.com.bw_goover_01.demo03.CustomView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/cs_id"/>

</LinearLayout>

    TabLayout + ViewPager + Fragment

     Main4Activity

     

package bw.com.bw_goover_01.demo04;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import bw.com.bw_goover_01.R;

/**
 * TabLayout + ViewPager + Fragment
 */
public class Main4Activity extends AppCompatActivity {

    //导航
    private TabLayout mTabLayout;
    private List<String> titles = new ArrayList<>();//标题

    //视图页面
    private ViewPager mViewPager;
    private List<Fragment> data = new ArrayList<>();//视图
    private MyAdapter adapter;//适配器

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

        mTabLayout = (TabLayout) findViewById(R.id.table_layout_id);
        mViewPager = (ViewPager) findViewById(R.id.view_pager_id);

        //TODO 初始化标题
        titles.add("第一个页面");
        titles.add("第二个页面");
        titles.add("第三个页面");
        titles.add("第四个页面");
        //TODO 初始化数据源
        for(int i=0;i<4;i++)
        {
            data.add(new MyFragment());
        }

        //TODO 初始化适配器
        adapter = new MyAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(adapter);

        //TODO 将TabLayout 和ViewPager 绑定在一起
        mTabLayout.setupWithViewPager(mViewPager);
    }

    //TODO 自定义适配器
    class MyAdapter extends FragmentPagerAdapter
    {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }
        @Override
        public int getCount() {
            return data.size();
        }
        //TODO 设置TabLayout 中的标题  -- ViewPager和TabLayout的结合
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
}

    MyFragment

    

package bw.com.bw_goover_01.demo04;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import bw.com.bw_goover_01.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {


    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

}

    布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="bw.com.bw_goover_01.demo04.Main4Activity">

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/table_layout_id"
        />

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager_id"
        />

</LinearLayout>

  fragment_my

  

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="bw.com.bw_goover_01.demo04.MyFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

 通过aidl 获取服务端中, 返回的数据, 显示在TextView中

 Main5Activity

  

package bw.com.bw_goover_01.demo05;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import bw.com.bw_goover_01.R;
import bw.com.serverapp.MyAidl;

/**
 * 通过aidl 获取服务端中, 返回的数据, 显示在TextView中
 */
public class Main5Activity extends AppCompatActivity {

    private TextView mTv;

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

        mTv = (TextView) findViewById(R.id.tv_id);
    }

    //TODO 检测服务绑定是否成功
    private ServiceConnection connection  = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //服务绑定成功的回调方法
            try {
                MyAidl myAidl = MyAidl.Stub.asInterface(service);
                String str = myAidl.getInfo();//获取数据
                mTv.setText(str);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    //TODO 绑定服务
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent();
        intent.setAction("com.bw.1511c.aidl");
        //5.0 以后, 指定包名
        intent.setPackage("bw.com.serverapp");

        bindService(intent,connection,BIND_AUTO_CREATE);
    }

    //TODO 解绑服务
    @Override
    protected void onStop() {
        super.onStop();
        unbindService(connection);
    }
}

  aidl

 

// MyAidl.aidl
package bw.com.serverapp;

// Declare any non-default types here with import statements
//TODO 接口的声明和方法的申明都没有修饰符
interface MyAidl {
    String getInfo();
}

     serverapp

    MainActivity

  

package bw.com.serverapp;

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);
    }
}

   MyService

package bw.com.serverapp;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;

/**
 * 自定义的服务
 */

public class MyService  extends Service{

    MyAidl.Stub stub = new MyAidl.Stub() {
        @Override
        public String getInfo() throws RemoteException {

            return "鹅鹅鹅, 曲项向天歌,白毛浮绿水,红掌拨清波!!!";
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return stub;
    }
}

 布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="bw.com.serverapp.MainActivity">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

  Aidl

 

// MyAidl.aidl
package bw.com.serverapp;

// Declare any non-default types here with import statements
//TODO 接口的声明和方法的申明都没有修饰符
interface MyAidl {
    String getInfo();
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值