2012-03-24 142 views
6

在我的水平LinearLayout中,我有一個TextEdit和一個ImageButton。 ImageButton和TextEdit一樣高。ImageButton:Force square icon(height = WRAP_CONTENT,width =?)

我希望ImageButton和它一樣寬。

在它看起來像的ImageButton的寬度的時刻是當沒有縮放(ImageButton的寬度[像素] =未縮放可繪製寬度[像素]),如:

example

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/txtName" 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/btSet" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:scaleType="fitEnd" 
     android:src="@drawable/pin2" /> 

</LinearLayout> 

如何它應該看起來像:

objective

+1

只是有點尖。將layout_weight設置爲視圖時,渲染速度更快,以將寬度設置爲0dp。 – 2012-04-06 14:18:31

+0

@BananaNutTruffles:謝謝!我不知道那件事。 – SecStone 2012-04-06 14:25:28

+0

您是否找到解決方案? – rekire 2012-11-16 14:13:25

回答

-3

你可以調整使用像素的圖像按鈕...像下面...

`android:layout_width="5px" 
    android:layout_height="5px"` 
+0

Im相當肯定,他希望它是可擴展的 – 2012-03-24 14:49:49

+0

沒錯。)(然而,這不是問題描述中說明) – SecStone 2012-03-24 15:09:24

0

只需使用weightSum到devide的相應控件的大小...

<?xml version="1.0" encoding="utf-8" ?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

    <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="1.0" > 

    <EditText 
     android:id="@+id/txtName" 
     android:layout_weight="0.8" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:inputType="text" 
     android:singleLine="true"/> 

    <ImageButton 
     android:id="@+id/btSet" 
     android:layout_weight="0.2" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:scaleType="fitEnd" 
     android:src="@drawable/pin2" /> 

    </LinearLayout> 
</LinearLayout> 

希望它會幫助你。

+0

謝謝您的建議。如果應用在小屏幕上運行,我可能需要25%或30%的空間將按鈕顯示爲方塊。這個問題還有解決方案嗎? – SecStone 2012-03-24 19:06:16

2

使用

android:scaleType="fitCenter" 

android:scaleType="centerInside"

在ImageButton的XML文件中

+0

感謝您的建議。我應該爲'android:layout_width'和'android:layout_height'使用哪些值? – SecStone 2012-04-05 08:32:26

+0

您可以根據您的要求使用填充父項或包裝內容... !!! 請檢查答案,如果你滿意... !!! – 2012-04-16 05:23:10

19

試試這個,我覺得這應該工作:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:orientation="horizontal" > 

    <ImageButton 
     android:id="@+id/btSet" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:scaleType="centerInside" 
     android:adjustViewBounds="true" 
     android:src="@drawable/pin2" /> 

    <EditText 
     android:id="@+id/txtName" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toLeftOf="@id/btSet" /> 

</RelativeLayout> 

釋:centerInside將確保該圖像將在ImageButton的範圍內按比例縮放。 adjustViewBounds="true"會...好吧,如果圖像需要縮放,請調整視圖的邊界。

+0

感謝您的建議。不幸的是它無法正常工作(屏幕截圖:http://i.imgur.com/W8iFu.png)。你知道任何其他解決方案嗎? – SecStone 2012-04-06 06:43:09

+0

我編輯了我的答案,試一下。此外,你可以上傳你使用的按鈕drawable(如果這不起作用)? – 2012-04-06 15:44:53

+0

感謝您的更新答案。它似乎還沒有工作(截圖:http://i.imgur.com/Awbat.png)。我的drawable(_pin2_):http://i.imgur.com/eflnh.png – SecStone 2012-04-06 17:28:57

5

嘗試添加

adjustViewBounds = 「真」

到ImageButton的,應當修剪多餘的寬度


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/txtName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/btSet" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:adjustViewBounds="true" 
     android:src="@drawable/pin" /> 
</LinearLayout> 
+0

感謝您的建議。不幸的是它無法正常工作(屏幕截圖:http://i.imgur.com/W8iFu.png)。你知道任何其他解決方案嗎? – SecStone 2012-04-06 09:01:48

+0

嘗試移除標記scaleType並將layout_width和height設置爲wrap_content – 2012-04-06 09:03:45

+0

您嘗試過嗎,先生?我在我的嘗試,它的工作。 – 2012-04-06 09:24:52

0
<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:weightSum="1.0" > 

<EditText 
    android:id="@+id/txtName" 
    android:layout_weight="0.75" // this work like percentage adjust as u want 70 or 75 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:inputType="text" 
    android:singleLine="true"/> 

<ImageButton 
    android:id="@+id/btSet" 
    android:layout_weight="0.25" // this work like percentage adjust as u want 25 or 30 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:scaleType="fitEnd" 
    android:src="@drawable/pin2" /> 

</LinearLayout> 
0

只是將下面的行添加到您的ImageButton和額外的背景將消失:

android:layout_gravity="center|clip_horizontal" 

希望這個作品

0

恰好碰到了這樣的事情和其他的建議沒有幫助。有什麼幫助是在ImageButton中設置填充:

android:padding="5dp" 

除此之外,我沒有觸及原始佈局。按鈕成爲了我測試了3個仿真器(3.2,4.4.2,5.1)

0

我想我自己,它的工作原理

只要做到以下幾點...

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="0dp" 
     android:layout_weight="5" 
     android:layout_height="wrap_content" 
     android:inputType="text"/> 

    <ImageButton 
     android:id="@+id/imageButton" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

使用佈局方重量比5:1一個LinearLayout內部爲EditTextImageButton

OUTPUT

1

我可能是晚會有點晚了。 但是,通過重寫onMeasure()可以輕鬆實現此行爲。 這是它會看起來像:

public class MySquareImageButton extends ImageButton { 
    public MySquareImageButton(Context context) { 
     super(context); 
    } 

    public MySquareImageButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MySquareImageButton(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     //(1)if you want the height to match the width 
     super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
     //(2)if you want the width to match the height 
     //super.onMeasure(heightMeasureSpec, heightMeasureSpec); 
    } 
} 

然後你只需與此自定義更換你的XML的ImageButton的:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/txtName" 
     android:layout_width="1dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <com.whatever_your_package.MySquareImageButton 
     android:id="@+id/btSet" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:scaleType="fitEnd" 
     android:src="@drawable/pin2" /> 

</LinearLayout> 

你會簡單地把WRAP_CONTENT至寬度或高度,取決於你想要決定你的按鈕的大小。 在這種情況下,你希望你的按鈕,它的高度換到的圖像,並且該寬度只是高度匹配,你會使用

 android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

,並使用

 //(2)if you want the width to match the height 
     //super.onMeasure(heightMeasureSpec, heightMeasureSpec); 
相關問題