2013-10-24 234 views
14

我在佈局中的一個使用Button的尺寸減小。我希望Button尺寸與文字/標籤尺寸一樣小。安卓:按鈕

我正在使用以下代碼。

<Button 
android:text="@string/cancle_button" 
android:id="@+id/button1" 
android:background="@drawable/back_button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="center" 
android:textSize="12sp" 
android:textColor="#ffffff" 
android:textStyle="bold" /> 

但我想要Button高度與文本/標籤高度相同。每當我減少或增加文字高度時,高度保持不變。我應該如何解決這個問題?

+2

當您在背景中傳遞圖像時,所以按鈕大小將取決於圖像大小和文本大小。所以如果你的文字大小比圖像小,它不會減少。但是,如果文字大小比圖像大,那麼它會變得適合文字。這一切都是因爲「wrap_content」屬性。 – Arshu

+1

非常感謝Arshad,但是我在我的drawable文件夾中使用.xml格式的漸變繪製。 –

+0

即使這樣,漸變也具有固定的大小和形狀。所以作爲圖像本身。 – Arshu

回答

1

可以使用相對佈局,以實現這一目標。檢查此示例代碼,

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="TextView" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/textView1" 
     android:layout_alignTop="@id/textView1" 
     android:layout_alignBottom="@id/textView1" 
     android:text="Button" /> 

</RelativeLayout> 
3

使用可以在這種情況下,或者如果你服用文本視圖圖像視圖在這種情況下,這樣你就可以採取適當尺寸的圖像,這樣它會通過這些措施沒有變得更藍,你可以爲圖像設置小型最好的辦法是你可以把圖像與您要創建您可以通過按鈕的高度設置爲這是與您的標籤的高度特定值解決這個問題的好辦法

+0

在這個要求我使用漸變形狀繪製在我的文件夾繪製感謝 –

1

該大小。

<Button 
android:text="CANCEL" 
android:id="@+id/button1" 
android:background="@drawable/back_button" 
android:layout_width="wrap_content" 
android:layout_height="20sp" 
android:gravity="center" 
android:textSize="15sp" 
android:textColor="#ffffff" 
android:textStyle="bold" /> 
+1

答覆,是否有任何其他方式在那裏按鈕會自動調整大小,而不硬編碼它。我希望在文字大小如果你的變化而變化 –

+0

按鈕大小應改變文本的大小,然後隨着高度設置爲包裝內容,它會自動調整該高度 – jyomin

+0

它不是調整按鈕的高度是相同的。 –

82

問題被問了已經差不多一年了。但我現在面臨這個問題並得到了解決方案。 :) 可能會幫助某人。

您可以通過爲按鈕組件設置android:minHeight="0dp"實現這一目標。

而我想這種行爲是因爲有一些默認值(可能是48dp)由框架考慮最小的觸摸面積設置的按鈕部件。

+4

絕對如此。明智的解決方案,一個簡單的問題。 – tasomaniac

+2

這應該被標記爲正確的 – Redman

+1

謝謝@Sreekanth –