2016-05-14 96 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;