本文介绍了如何在对话框中使开关按钮居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在对话框中切换按钮中心,但是失败了...

I want to switch button center in dialog,but it's failed...

为什么?

Switch sw = new Switch(MainActivity.this);
sw.setTextOn("start");
sw.setTextOff("close");
sw.setGravity(Gravity.CENTER_HORIZONTAL);

AlertDialog.Builder myDialog = new AlertDialog.Builder(MainActivity.this);
myDialog.setTitle("title");
myDialog.setMessage("message");
myDialog.setView(sw);

推荐答案

尝试以下代码

        Switch sw = new Switch(MainActivity.this);
        sw.setTextOn("start");
        sw.setTextOff("close");


        LinearLayout linearLayout = new LinearLayout(MainActivity.this);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        linearLayout.addView(sw);


        AlertDialog.Builder myDialog = new    AlertDialog.Builder(MainActivity.this);
        myDialog.setTitle("title");
        myDialog.setMessage("message");
        myDialog.setView(linearLayout);


        myDialog.show();

这篇关于如何在对话框中使开关按钮居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 05:51