游戏技术文章

Android第五天-->创建自定义控件

时间:2017-4-10 12:45:35  作者:棋牌资源网  来源:棋牌资源网  查看:8912  评论:0
内容摘要:1、仿 iPhone 的风格,在界面的顶部放置一个标题栏。1、仿 iPhone 的风格,在界面的顶部放置一个标题栏。
复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#2197db"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            android:id="@+id/title_back"
            android:layout_width="90dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:text="返回"
            />

        <TextView
            android:id="@+id/title_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:text="标题"
            android:textColor="#fff"
            android:textSize="24sp"
            />
        <Button
            android:id="@+id/title_edit"
            android:layout_width="90dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:text="确定"
            />

    </LinearLayout>
</RelativeLayout>
复制代码

Android第五天-->创建自定义控件

标题栏布局已经编写完成,剩下的就是如何在程序中使用这个标题栏。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/title" />
</LinearLayout>
//我们只需要通过一行 include语句将标题栏布局引入进来就可以了。

然后在 MainActivity 中将系统自带的标题栏隐藏掉

复制代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
复制代码

我们还是需要在每个活动中为这些控件单独编写一次事件注册的代码。比如说标题栏中的返回按钮,其实不管是在哪一个活动中,这个按钮的功能都是相同的,即销毁掉当前活动,这种情况最好是使用自定义控件的方式来解决。

新建自定义的标题栏控件:

 

复制代码
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
}
}

 /*

我们重写了 LinearLayout 中的带有两个参数的构造函数,在布局中引入 TitleLayout控件就会调用这个构造函数。然后在构造函数中需要对标题栏布局进行动态加载,这就要借
助 LayoutInflater 来实现了。通过 LayoutInflater 的 from()方法可以构建出一个 LayoutInflater对象,然后调用 inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参数,第一个参数是要加载的布局文件的 id,这里我们传入 R.layout.title,第二个参数是给加载好的布局再添加一个父布局,这里我们想要指定为 TitleLayout,于是直接传入 this

*/

 
复制代码

 

在布局文件中添加这个自定义控件

 

 

复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.xxxxxx.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.xxxxxx.TitleLayout>
</LinearLayout>
复制代码

 

我们来尝试为标题栏中的按钮注册点击事件,修改 TitleLayout中的代码

复制代码
public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title, this);
        Button titleBack = (Button) findViewById(R.id.title_back);
        Button titleEdit = (Button) findViewById(R.id.title_edit);

        titleBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });

        titleEdit.setOnClickListener(new OnClickListener() {
            public static final String TAG = "";

            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "重新运行程序", Toast.LENGTH_SHORT).show();
                Log.i(TAG, "111 ");
            }
        });
    }

}
复制代码

标签:Android第五天创建自定义控件 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved