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里面增加其他方法 。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值