简体   繁体   English

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

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

I'm trying to create a custom style for my Android Toggle Buttons. 我正在尝试为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. 我将wrap_content和padding以及margin的高度设置为0,但是按钮的大小仍然与默认的Toggle Button一样大。

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 link to 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 编辑:图像和代码

If you are just trying to change the way the text is rendered inside of the ToggleButton , then you need to adjust the android:padding and android:gravity parameters 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:gravity parameter 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. 这将使文本与ToggleButton的左侧对齐。 However, this will also align it to the top by default, as gravity effects both the x and y axis. 但是,默认情况下,这也将使其与顶部对齐,因为重力会影响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 only which 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内部文本对齐的最佳方法。

This will give you the width of the string ie; 这将给你字符串的宽度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. 让我们说切换按钮的父布局是LinearLayout。 so it should go like: 所以它应该像:

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的宽度和高度设置为您计算的值。

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

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

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. 我遇到了完全相同的问题,并且因为这个原因几乎放弃了使用ToggleButton。 Calling setMinWidth(int) and setMinHeight(int) in my code had no effect for me, nor did anything else I tried. 在我的代码中调用setMinWidth(int)和setMinHeight(int)对我没有任何影响,也没有尝试过其他任何操作。 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: 但是当我在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. 只要在xml中设置minWidth和minHeight,就​​可以以编程方式设置填充。 Defining your widgets in xml is very easy to do btw. 用bml定义你的小部件非常容易。 Most people do it using View.inflate() or myinflater.inflate(), so Google those terms and you will find examples. 大多数人使用View.inflate()或myinflater.inflate()来做这些,所以谷歌这些术语和你会找到例子。

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

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