2015-12-22 67 views
0

我想只有一個ImageButton水平和垂直居中,但具有特定百分比的響應寬度,比方說70%,並根據其寬度設置其高度(所以它不會是拉伸)Android ImageButton居中響應寬度

我做到目前爲止居中,我試圖實現響應寬度。

這是代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#30CCA4"> 

<ImageButton 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight=".30" 
    android:orientation="vertical" 
    android:layout_gravity="center" 
    android:id="@+id/imageButton" 
    android:src="@drawable/splash_image" 
    android:background="@null" /> 

我該怎麼辦的響應寬?

謝謝

回答

0

試試這個。

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

    <ImageButton 
     android:id="@+id/img_bt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#FF4081" /> 

</LinearLayout> 

代碼:

ImageButton img_bt = (ImageButton) view.findViewById(R.id.img_bt); 
    Display display = getActivity().getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight(); 
    LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width/10*7, width/10*7); 
    img_bt.setLayoutParams(parms); 
+0

非常感謝廣杜安 –