2017-02-18 1 views
0
で広場のImageButton

私は、Androidに新しいですので、しばらくお待ちください:)アンドロイド - GridLayoutの

私のメイン画面/主な活動で、私はグリッド2×Nを持つようにしたいので、私は2つの列とN行を持つようにしたいです。グリッド内のアイテムはImageButtonです。

ImageButtonsのimgがic_launcher - >次にグリッドの項目が問題ない場合、すべてうまくいきます。しかし、グラフィック(500x500px)を置くと「タイル」が画面より大きくなり、タイルに合わせてこの「拡大」する方法がわからないので、ic_launcherを持っているときと同じサイズにすることができます。ここで

はコードです:すべてがうまくあるときここ

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:columnCount="2" 
    android:rowCount="3"> 

    <ImageButton 
     android:layout_height="wrap_content" 
     app:srcCompat="@mipmap/ic_launcher" 
     android:scaleType="centerCrop" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1" 
     android:layout_width="wrap_content" 
     android:padding="1dp" 
     android:elevation="0dp" 
     android:layout_gravity="fill" /> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@mipmap/ic_launcher" 
     android:layout_gravity="fill" 
     android:layout_columnWeight="1" 
     android:layout_rowWeight="1"/>  

</GridLayout> 

は画面です:

enter image description here

私はこのサイズに合うように、私のIMGを拡張するために何ができますか?

重要なこと:私のimgは正方形であるので、このimgを正方形のサイズに拡大したいと思っています。

編集:私は私のIMGを使用する場合、それは次のようになります。私は、私が直面した同じ問題のために微調整を発見した

enter image description here

+0

..私の試みです'。自分の写真でどのように見えるのか教えてください。 – JonZarate

+0

なぜgridLayout?内に2つのImageButtonを使用していますか?意味がありません。 –

+0

@JonZarate私はアップデートをしました、私はすべてscaleTypeをテストしました –

答えて

1

は、私はあなたが正しい `アンドロイドを使用していない賭け

MainActivity.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 
    private GridView gridView; 
    ArrayList<Integer> integerRes = new ArrayList<>(); 
    { 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 
     integerRes.add(R.drawable.bbent); 


    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     gridView = (GridView) findViewById(R.id.grid); 
     gridView.setAdapter(new GridAdapter()); 


    } 

    class GridAdapter extends BaseAdapter { 

     @Override 
     public int getCount() { 
      return integerRes.size(); 
     } 

     @Override 
     public Object getItem(int i) { 
      return i; 
     } 

     @Override 
     public long getItemId(int i) { 
      return i; 
     } 

     @Override 
     public View getView(int i, View view, ViewGroup viewGroup) { 
      ImageView imageView=new ImageView(getApplicationContext()); 

      imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,440)); 
      // for better result in various devices calculate width at runtime and measure height accordingly 

      imageView.setImageResource(integerRes.get(i)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      return imageView; 
     } 
    } 

} 

grid.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <GridView 
     android:id="@+id/grid" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:numColumns="2"/> 
</LinearLayout> 

enter image description here

+0

うわー、これは私が必要と思うものだと思う。私が家に帰るとき、私はそれを試して、あなたに "受け入れる"ようにします。ありがとう! –

+0

すべては問題ありませんが、幅と同じ高さに設定するにはどうすればよいですか? "440"と入力するのではなく、 –

+0

は実行時に画面の幅を取って2で割ります。 –

1

::

としてのLinearLayoutでのImageButtonをラップ::

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

    <LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:orientation="vertical" 
    android:weightSum="10" 
    android:gravity="center"> 

     <ImageButton 
    android:layout_height="wrap_content" 
    app:srcCompat="@mipmap/ic_launcher" 
    android:scaleType="centerCrop" 
    android:layout_weight="6" 
    android:layout_width="0dp" 
    android:layout_gravity="fill" /> 

    </LinearLayout> 

    <LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:orientation="horizontal" 
    android:weightSum="10" 
    android:gravity="center"> 

     <ImageButton 
    android:layout_height="wrap_content" 
    app:srcCompat="@mipmap/ic_launcher" 
    android:scaleType="centerCrop" 
    android:layout_width="0dp" 
    android:layout_weight="6" 
    android:padding="1dp" 
    android:layout_gravity="fill" /> 

    </LinearLayout> 

</LinearLayout 

必要に応じて、ImageButtonまたはLinearLayoutに余白とパディングを適用します。これは単なるコンセプトです。 scaleType:ここ

+0

いいえ、それは動作することができますが、私はGridLayoutを持っていたい、リニアではありません。あなたのソリューションを試してみることが悪ければ、私は他のソリューションをチェックします。助けてくれてありがとう –