繁体   English   中英

Android Toggle Button自定义大小/填充/间距

[英]Android Toggle Button custom size/padding/spacing

我正在尝试为Android Toggle Buttons创建自定义样式。 更改颜色和文本没有问题,但我无法更改大小/填充/间距或其他任何内容,使它们默认显示为不必要的大。 我将wrap_content和padding以及margin的高度设置为0,但是按钮的大小仍然与默认的Toggle Button一样大。

你是否有人知道我要更改哪些参数以删除按钮的文本和边框之间不必要的间距?

这是我想要实现的图像的链接 从左到右:默认ToggleButton,我当前的ToggleButton和我想要的ToggleButton。

这是我的代码(因为我动态添加这些按钮,没有xml)

ToggleButton button = new ToggleButton(getContext());
button.setLayoutParams(new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
button.setId(IDCreator.getID());
button.setText(tag);
button.setTextOff(tag);
button.setTextOn(tag);
button.setGravity(Gravity.LEFT);
button.setPadding(0, 0, 0, 0);
button.setBackground(getContext().getResources().getDrawable(R.drawable.toggle_selector));

谢谢你的帮助。 亲切的问候。

编辑:图像和代码

如果您只是想改变文本在ToggleButton呈现方式,那么您需要在XML中为您的按钮调整android:paddingandroid:gravity参数。

要更改填充,首先需要使文本远离默认居中(因为当它的居中填充无法生效时)。 你可以通过android:gravity参数来实现这一点,就像CSS中的text-align一样。

例如;

<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left">
</ToggleButton>

这将使文本与ToggleButton的左侧对齐。 但是,默认情况下,这也将使其与顶部对齐,因为重力会影响x轴和y轴。

如果要垂直和左侧水平对齐中心,则执行以下操作:

<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center">
</ToggleButton>

这将使您以垂直方向居中,但在水平刻度上完全向左设置。 这将使文本位于按钮边缘的顶部并且看起来没有吸引力。 您需要将对齐与文本的填充结合使用,以便将其准确地放在您想要的位置。

例如:

<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding_left="10dp"
android:gravity="left|center">
</ToggleButton>

这会在左侧添加填充,这意味着您可以根据需要将其设置为远离边框。

您可以使用每边的填充和重力来缩放文本以满足您的需要,但这将是控制ToggleButton内部文本对齐的最佳方法。

这将给你字符串的宽度ie; 你的案子中的文字。

Paint paint = toggleButton.getPaint();
float length = paint.measureText(YOUR_STRING);

这将为您提供屏幕上的字符串宽度。 现在通过LayoutParams设置切换按钮的宽度。

让我们说切换按钮的父布局是LinearLayout。 所以它应该像:

toggleButton.setLayoutParams(new LayoutParams(WIDTH_YOU_CALCULATED,LinearLayout.LayoutParams.WRAP_CONTENT));         

这会将ToggleButton的宽度和高度设置为您计算的值。

我意识到这已经过时了,但也许使用setScaleX({提供浮点数0.0 - 1.0})和setScaleY({提供浮点数0.0 - 1.0})可能是原帖的答案(如何更改切换按钮的大小) )。

请改用CheckedTextView: https ://stackoverflow.com/a/36084999/377320它只是一个带有选中状态的常规TextView,所以没有奇怪的填充等。

您需要在xml文件中定义ToggleButton并确保包含

 android:minHeight="0dp"
 android:minWidth="0dp"

作为属性。 我遇到了完全相同的问题,并且因为这个原因几乎放弃了使用ToggleButton。 在我的代码中调用setMinWidth(int)和setMinHeight(int)对我没有任何影响,也没有尝试过其他任何操作。 但是当我在xml中设置这些属性时,我可以使用填充来控制ToggleButton的大小,但是请注意我还使用xml修改了填充,如下所示:

android:paddingTop="6dp"
android:paddingBottom="6dp"

只要在xml中设置minWidth和minHeight,就​​可以以编程方式设置填充。 用bml定义你的小部件非常容易。 大多数人使用View.inflate()或myinflater.inflate()来做这些,所以谷歌这些术语和你会找到例子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM