2014-12-03 148 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" />