java Android 切换按钮自定义大小/填充/间距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25410227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:59:24  来源:igfitidea点击:

Android Toggle Button custom size/padding/spacing

javaandroidlayouttogglebutton

提问by Androidicus

I'm trying to create a custom style for my Android Toggle Buttons. Changing colour and text was no problem but I'm having trouble changing the size/padding/spacing or whatever it is that let them appear so unnecessarily large by default. I've set height to wrap_content and padding and margin to 0, but the size of the button is still as a big as the default Toggle Button.

我正在尝试为我的 Android 切换按钮创建自定义样式。更改颜色和文本没有问题,但我无法更改大小/填充/间距或任何让它们在默认情况下显得不必要地大的东西。我已将高度设置为 wrap_content,将填充和边距设置为 0,但按钮​​的大小仍然与默认的切换按钮一样大。

Does anyone of you know what parameters I've to change to remove to unnecessary spacing between the button's text and border?

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

Here's a linkto an image of what I want to achive. From left to right: Default ToggleButton, my current ToggleButton and the kind of ToggleButton I want.

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

Here's my code (as I add those buttons dynamically, no xml)

这是我的代码(因为我动态添加这些按钮,没有 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));

Thanks for your help. Kind regards.

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

Edit: Image and Code

编辑:图像和代码

回答by Steven Toews

If you are just trying to change the way the text is rendered inside of the ToggleButton, then you need to adjust the android:paddingand android:gravityparameters in the XML for your button.

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

To change the padding, you first need to take the text away from being centered by default (because when it's centered padding has no ability to take effect). You do this by the android:gravityparameter which is like text-align in CSS.

要更改填充,您首先需要使文本远离默认居中(因为居中时填充无法生效)。您可以通过android:gravity类似于 CSS 中的 text-align的参数来执行此操作。

For example;

例如;

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

This will align the text to the left of the ToggleButton. However, this will also align it to the top by default, as gravity effects both the x and y axis.

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

If you want to align to the center vertically and to the left horizontally, you would do the following:

如果要垂直居中对齐,水平居左对齐,可以执行以下操作:

<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>

This will give you it centered vertical, but fully set left on the horizontal scale. This will make the text go ontop of the edge of your button and look unappealing. You need to use the alignments in conjunction with a padding for the text in order to get it exactly where you want.

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

For example:

例如:

<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>

This adds padding to the left onlywhich means you can set it as much away from the border as you like.

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

You can play around with the padding for each side and the gravity in order to scale the text to your needs, but this would be the best approach to control the ToggleButton's inner text alignment.

您可以调整每一侧的填充和重力,以便根据您的需要缩放文本,但这将是控制 ToggleButton 的内部文本对齐方式的最佳方法。

回答by Maverick

This will give you the width of the string ie; text in your case.

这将为您提供字符串的宽度,即;你的情况下的文字。

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

This gives you the width of the string it would take on the screen. Now set the width of the toggle button via LayoutParams.

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

Lets say parent layout of your toggle button is LinearLayout. so it should go like:

假设您的切换按钮的父布局是 LinearLayout。所以它应该是这样的:

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

This will set the width and height of your ToggleButton to what you calculated.

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

回答by rvs

I realize this is outdated, but perhaps using setScaleX({provide a float 0.0 - 1.0}) and setScaleY({provide a float 0.0 - 1.0}) might be the answer for the original post (how to change the size of the toggle button).

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

回答by Alyoshak

You need to define your ToggleButton in an xml file and make sure you include

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

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

as attributes. I had the exact same problem, and almost gave up on using ToggleButton for that reason. Calling setMinWidth(int) and setMinHeight(int) in my code had no effect for me, nor did anything else I tried. But when I set these attributes in the xml I was then able to control the size of the ToggleButton with padding, although be aware that I also modified the padding in with xml, like so:

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

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

You might be able to set the padding programmatically as long as you set the minWidth and minHeight in the xml. Defining your widgets in xml is very easy to do btw. Most people do it using View.inflate() or myinflater.inflate(), so Google those terms and you will find examples.

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

回答by Martin Vysny

Use CheckedTextView instead: https://stackoverflow.com/a/36084999/377320It is just a regular TextView with checked state, so no weird paddings etc.

使用 CheckedTextView 代替:https://stackoverflow.com/a/36084999/377320它只是一个具有选中状态的常规 TextView,因此没有奇怪的填充等。