欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 自定义LinearLayout(简陋开关按钮的实现)

程序员文章站 2022-03-07 16:10:18
最简单的实现方式:布局xml

最简单的实现方式:

Android 自定义LinearLayout(简陋开关按钮的实现)

 

 

 

Android 自定义LinearLayout(简陋开关按钮的实现)

 

 

 

 

布局xml

<?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:onClick="bt_qiehuan"
    android:layout_width="350dp"
    android:layout_height="150dp"
    android:orientation="vertical"
    tools:context=".make_layout.ViewLayoutActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:background="#455D9D"
        android:orientation="horizontal">
        <!--开设置-->
        <LinearLayout
            android:id="@+id/ll_morentype1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="开"
                android:textColor="@color/white"
                android:textSize="30sp" />

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
        <!--关设置-->
        <LinearLayout
            android:id="@+id/ll_morentype2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone">

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="关"
                android:textColor="@color/white"
                android:textSize="30sp"

                />

        </LinearLayout>

    </LinearLayout>
</LinearLayout>

 

 代码:

public class ViewLayoutActivity extends AppCompatActivity {
    private boolean qiehuan_boolean = false;  //默认 true:开 false:关
    LinearLayout ll_morentype1,ll_morentype2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_layout);
        ll_morentype1=findViewById(R.id.ll_morentype1);
        ll_morentype2=findViewById(R.id.ll_morentype2);
    }



    public void bt_qiehuan(View view) {
        if (qiehuan_boolean == true) {
            ll_morentype1.setVisibility(View.VISIBLE);
            ll_morentype2.setVisibility(View.GONE);
            qiehuan_boolean = false;
        } else {
            ll_morentype1.setVisibility(View.GONE);
            ll_morentype2.setVisibility(View.VISIBLE);
            qiehuan_boolean = true;
        }
    }
}

这样就可以简单的实现开关的效果了

当然,能封装一下就好了:

1.将上面的逻辑和布局复制一份:新建一个类TitleLinearLayout 

public class TitleLinearLayout extends LinearLayout  {
    private boolean qiehuan_boolean = false;  //默认 true:开 false:关
    LinearLayout ll_morentype1,ll_morentype2,ll_qiehuan;
    Tabstutas tabstutas;
    public TitleLinearLayout(Context context) {
        super(context);
    }

    public TitleLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.activity_view_layout,this);
        ll_morentype1=findViewById(R.id.ll_morentype1);
        ll_morentype2=findViewById(R.id.ll_morentype2);
        ll_qiehuan=findViewById(R.id.ll_qiehuan);
        ll_qiehuan.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if(tabstutas!=null){
                    if (qiehuan_boolean == true) {
                        ll_morentype1.setVisibility(View.VISIBLE);
                        ll_morentype2.setVisibility(View.GONE);
                        qiehuan_boolean = false;
                        tabstutas.cheak(true);
                    } else {
                        ll_morentype1.setVisibility(View.GONE);
                        ll_morentype2.setVisibility(View.VISIBLE);
                        qiehuan_boolean = true;
                        tabstutas.cheak(false);
                    }
                }

            }
        });
    }

public void setlistnear(Tabstutas tabstutas){
        this.tabstutas=tabstutas;
}
     interface Tabstutas{
        void cheak(boolean stutas);
    }
}

 2.新建一个布局

<?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:id="@+id/ll_qiehuan"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".make_layout.ViewLayoutActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:background="#455D9D"
        android:orientation="horizontal">
        <!--开设置-->
        <LinearLayout
            android:id="@+id/ll_morentype1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="开"
                android:textColor="@color/white"
                android:textSize="30sp" />

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />
        </LinearLayout>
        <!--关设置-->
        <LinearLayout
            android:id="@+id/ll_morentype2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone">

            <ImageButton
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="关"
                android:textColor="@color/white"
                android:textSize="30sp"

                />

        </LinearLayout>

    </LinearLayout>
</LinearLayout>

2.测试页面使用

 

<?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="350dp"
    android:layout_height="150dp"
    android:orientation="vertical"
    tools:context=".make_layout.ViewLayoutActivity">
   <com.example.test_01.make_layout.TitleLinearLayout
       android:id="@+id/ll_stutas"
    android:layout_width="200dp"
    android:layout_height="100dp"/>

</LinearLayout>
public class ViewLayoutActivity extends AppCompatActivity {
    TitleLinearLayout  ll_stutas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewlayout);
        ll_stutas=findViewById(R.id.ll_stutas);

       ll_stutas.setlistnear(new TitleLinearLayout.Tabstutas() {
           @Override
           public void cheak(boolean stutas) {
               Log.i("点击的状态", "cheak: "+stutas);
           }
       });

    }
}

简单封装了实现开关按钮的效果和状态输出,当然这样这是很简陋的,如果你有更好的改良方式都可以在自定义类TitleLinearLayout里面增加其他方法 。

本文地址:https://blog.csdn.net/qq_36771930/article/details/107864601