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

最简单的实现方式:

Android 自定义LinearLayout(简陋开关按钮的实现)_第1张图片

 

 

 

Android 自定义LinearLayout(简陋开关按钮的实现)_第2张图片

 

 

 

 

布局xml




    
        
        

            

            
        
        
        

            

            

        

    

 

 代码:

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.新建一个布局




    
        
        

            

            
        
        
        

            

            

        

    

2.测试页面使用

 



   

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

你可能感兴趣的:(Ui适配问题)