zoukankan      html  css  js  c++  java
  • 创建自定义控件

    一般来说,我们的程序有很多个标题栏,如果每个活动的布局中都编写一遍同样的标题栏代码,明显会导致大量代码重复。这个时候我们可以使用引入布局的方式来解决这个问题,新建一个布局title.xml。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/ic_launcher_background">
    
        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:text="Back"
            android:textAllCaps="false" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Title text"
            android:textSize="24sp" />
    
        <Button
            android:id="@+id/edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:text="Edit"
            android:textAllCaps="false" />
    
    </LinearLayout>
    

    接下来,我们只需要通过一行include语句将标题栏布局引进来就可以了。

    <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>
    

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

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //隐藏标题栏
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.hide();
            }
        }
    }
    

    引入布局解决了重复编写布局代码的问题,但是如果布局中有一些控件要求能够响应事件,我们还是要在每个活动中为这些控件单独编写一次事件注册的代码。比如标题栏中的返回按钮,其实不管在哪一个活动中,这个按钮的功能都是相同的。而如果在每一个活动中都需要重新注册一遍返回按钮的代码,无疑会增加很多重复代码,这种情况最好使用自定义控件。

    新建TitleLayout 继承LinearLayout,让它成为我们自定义的标题栏控件。首先我们重写LinearLayout中带有两个参数的构造方法,然后通过LayoutInflater的from方法构建一个LayoutInflater对象,紧接着使用inflate方法动态加载一个布局文件。

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

    然后我们在布局文件中加入这个自定义控件,值得注意的是,我们需要指明控件的完整类名,包名在这里不可以省略。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <com.example.uicustomviews.TitleLayout
            android:layout_width="match_parent"
            android:layout_height="50dp" />
    
    </LinearLayout>
    

    最后,我们为标题栏中的按钮注册点击事件,在TitleLayout 中添加以下代码

    Button back = findViewById(R.id.back);
    Button edit = findViewById(R.id.edit);
    
    back.setOnClickListener(view -> ((Activity) getContext()).finish());
    edit.setOnClickListener(view ->
                    Toast.makeText(getContext(), "you click Edit button", Toast.LENGTH_LONG).show();
    
  • 相关阅读:
    git(1)-git关联GitHub-windows-转载
    jenkins(4)-jenkins配置邮件通知
    jenkins(3)-linux下安装jenkins(yum install方式)
    【PAT甲级】1090 Highest Price in Supply Chain (25 分)(DFS)
    【PAT甲级】1087 All Roads Lead to Rome (30 分)(MAP【int,string】,邻接表,DFS,模拟,SPFA)
    【PAT甲级】1018 Public Bike Management (30 分)(DFS,SPFA)
    Educational Codeforces Round 61 (Rated for Div. 2) G(线段树,单调栈)
    Atcoder Grand Contest 032C(欧拉回路,DFS判环)
    Educational Codeforces Round 62 (Rated for Div. 2)E(染色DP,构造,思维,组合数学)
    Atcoder Grand Contest 031C(构造,思维,异或,DFS)
  • 原文地址:https://www.cnblogs.com/ngy-liupeng/p/15216708.html
Copyright © 2011-2022 走看看