2016-05-14 90 views
0

My app right now如何調整圖像大小以適應按鈕?

正如你所看到的,圖片膨脹了第一個按鈕不成比例。如何調整圖像,使其適合按鈕

我希望它看起來更像是this

凡圈會縮小,適合按鈕

這裏是我的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 

    tools:context="com.example.anthonypan.myapplication.MainActivity"> 


    <TableLayout 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true"> 

     <TableRow> 

      <Button 
       android:id="@+id/button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@drawable/selector" 
       android:textColor="@color/red" /> 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@drawable/selector" 
       android:textColor="@color/red" /> 

      <Button 
       android:id="@+id/button3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@drawable/selector" 
       android:textColor="@color/red" /> 
     </TableRow> 
    </TableLayout> 


</RelativeLayout> 

回答

0

添加它而不是表格。

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

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

    </LinearLayout> 
0

設置您的按鈕大小換行內容,意味着按鈕大小將取決於圖像大小。更好的是你可以修復你的按鈕大小。

<RelativeLayout 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" 
tools:context="com.example.anthonypan.myapplication.MainActivity"> 
<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true"> 

    <TableRow> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_weight="1" 
      android:background="@drawable/selector" 
      android:textColor="@color/red" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_weight="1" 
      android:background="@drawable/selector" 
      android:textColor="@color/red" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:layout_weight="1" 
      android:background="@drawable/selector" 
      android:textColor="@color/red" /> 
    </TableRow> 
</TableLayout> 

0

在您需要的屏幕截圖,它看起來像你希望所有的按鈕來佔據屏幕的1/3 。您可以輕鬆地通過先獲取的屏幕尺寸,這樣做:

Display display = getWindowManager().getDefaultDisplay(); // Your screen 
Point size = new Point(); 
display.getSize(size); 
int screenWidth = size.x; // The width of your screen 
int screenHeight = size.y; // The height of your screen 

接下來,獲取圖像的目標尺寸和寬度:

int imageWidth = imageView.getWidth(); 
float width = screenWidth/3; 
float scaleFactor = width/imageWidth; // This gets the scale factor to get the height 
float height = imageView.getHeight() * scaleFactor; 

現在,您可以設置的ImageButton的尺寸像這樣:

imageView.requestLayout(); 
imageView.getLayoutParams().height = height; 
imageView.getLayoutParams().width = width;