2014-12-03 115 views
-2

我已创建自定义按钮,并把它夹在抽拉文件夹中,代码如下它:减少自定义按钮尺寸

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#f50057" /> 
    <padding 
     android:left="30dp" 
     android:top="0dp" 
     android:right="30dp" 
     android:bottom="0dp" /> 
    <corners android:radius="5dp" /> 
</shape> 

我使用上述作为背景的按钮,该按钮被占用过多空间垂直事件虽然我已经把它填充为0dp。你能帮我减少按钮垂直占用的空间吗?

代码布局:

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/start_button" 
    android:background="@drawable/myrect" 
    android:focusable="false" 
    android:text="Start" 
    android:layout_below="@+id/expandview" 
    android:layout_centerHorizontal="true" /> 

enter image description here

+0

变化机器人:layout_height = “WRAP_CONTENT” 用固定值(EXP:机器人:layout_height = “48dp”) – Rami 2014-12-03 07:05:04

+0

'机器人:layout_height = “10dp”'Ñ尝试 – 2014-12-03 07:06:57

+0

PLZ检查此链接-http:/ /stackoverflow.com/questions/4361309/remove-space-between-buttons。我认为有帮助 – Sanket990 2014-12-04 05:44:39

回答

2

只要指定在布局XML按钮所需的高度和宽度。即

<Button 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:id="@+id/start_button" 
    android:background="@drawable/myrect" 
    android:focusable="false" 
    android:text="Start" 
    android:layout_below="@+id/expandview" 
    android:layout_centerHorizontal="true" /> 

如果你想这样做的绘制....然后

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="#f50057" /> 
    <padding 
     android:left="30dp" 
     android:top="0dp" 
     android:right="30dp" 
     android:bottom="0dp" /> 
    <corners android:radius="5dp" /> 
    <size 
     android:height="50dp" 
     android:width="50dp" /> 
</shape> 
+0

我不想在Button中更改,我希望它是全局的,并在标记中将其更改为ony。 – Psypher 2014-12-03 15:25:57

+0

使用标签形状xml ...它将使其成为一个全球性的大小...标记此答案,并接受它,如果它有帮助.. – 2014-12-04 05:10:44

1

Android的按钮视图的最小高度,所以如果你想要的东西时,你有

覆盖它
android:minHeight 

如在此代码:

<Button 
    android:id="@+id/btn_cancel" 
    android:layout_width="120dp" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="10dp" 
    android:minHeight="10dp" 
    android:paddingBottom="5dp" 
    android:paddingTop="5dp" 
    android:text="@string/cancel" 
    android:textColor="@color/white" /> 

然后,您可以明显看到layout_height参数,但如果您的值低于48 dp(默认最小高度),它将不起作用。例如,Button视图的最小宽度是64 dp。

+0

我不想在Button中更改,我希望它是全球性的,并改变它在标签中。 – Psypher 2014-12-03 15:25:40

+0

你为什么不想改变这个?用背景中的形状和minHeight参数创建一个样式可以吗? – Jejefcgb 2014-12-03 16:20:13

+0

这可能会做,如果它可能..但没有办法纠正它的形状。 – Psypher 2014-12-03 16:28:49

2

可以使用大小标记指定绘制和按键高度的尺寸,宽度将WRAP_CONTENT

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle"> 
<solid android:color="#f50057" /> 
<padding 
    android:left="30dp" 
    android:top="0dp" 
    android:right="30dp" 
    android:bottom="0dp" /> 
<corners android:radius="5dp" /> 
<size android:height="50dp" 
    android:width="50dp"/> 
</shape> 

,或者您可以指定高度和宽度这样

特定的按钮

只需在布局xml中指定所需的高度和宽度即可。即

<Button 
android:layout_width="50dp" 
android:layout_height="50dp" 
android:id="@+id/start_button" 
android:background="@drawable/myrect" 
android:focusable="false" 
android:text="Start" 
android:layout_below="@+id/expandview" 
android:layout_centerHorizontal="true" />