2016-05-14 19 views
0

My app right nowボタンに合わせて画像のサイズを変更する方法は?

ご覧のとおり、最初のボタンが膨らんでいます。

<?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> 
:それはボタン

私は円が縮小し、ボタンここ

を収まるところ、それはより多くのようthis

を見てみたいに収まるように、私は絵のサイズを変更するにはどうすればよい私のコードです

答えて

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を取るにしたいように、それが見えます。あなたは簡単に最初に、このように、画面サイズを取得することによってこれを行うことができます:あなたは今のImageButtonの寸法を設定することができ

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; 

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 

次に、画像のターゲットサイズと幅を取得しますように:

imageView.requestLayout(); 
imageView.getLayoutParams().height = height; 
imageView.getLayoutParams().width = width; 
関連する問題