2017-01-12 19 views
0

こんにちはこのタイプのレイアウトを開発したいのですが、グリッドビューやイメージビューをそのように配置するのは混乱しています。 まあ、私の質問は、私は私のフラグメントのメニューのこのタイプは、私がグリッドビューと画像とテキストを中心に

enter image description here

を働いているhttps://www.learn2crack.com/2014/01/android-custom-gridview.html

からGridViewのを実装した後、私はこのような画面

enter image description here

を得たacheiveことができる方法であります

gridViewを最初の画像のように見えるようにするにはどうすればよいですか?

私は本当にそれを達成したいブロックの中心にテキストとして。

grid_single.xml

<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="wrap_content" 
    android:orientation="vertical" 
    android:padding="5dp" > 

    <ImageView 
     android:id="@+id/grid_image" 
     android:layout_width="match_parent" 
     android:src="@drawable/denist_3" 
     android:layout_height="50dp"> 
    </ImageView> 

    <TextView 
     android:id="@+id/grid_text" 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:layout_gravity="center" 
     android:text="Dentist" 
     android:textSize="9sp" > 
    </TextView> 

</LinearLayout> 

main_act.xml:

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
     <GridView 
     android:numColumns="auto_fit" 
     android:layout_below="@+id/comsp" 
     android:gravity="center" 
     android:columnWidth="150dp" 
     android:stretchMode="columnWidth" 
     android:layout_width="fill_parent" 
     android:layout_height="600dp" 
     android:id="@+id/grid" 
     /> 

</RelativeLayout> 

Adapterclass.java

public class CustomGrid extends BaseAdapter{ 
    private Context mContext; 
    private final String[] web; 
    private final int[] Imageid; 

    public CustomGrid(Context c,String[] web,int[] Imageid) { 
     mContext = c; 
     this.Imageid = Imageid; 
     this.web = web; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return web.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View grid; 
     LayoutInflater inflater = (LayoutInflater) mContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (convertView == null) { 

      grid = new View(mContext); 
      grid = inflater.inflate(R.layout.grid_single, null); 
      TextView textView = (TextView) grid.findViewById(R.id.grid_text); 
      ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image); 
      textView.setText(web[position]); 
      imageView.setImageResource(Imageid[position]); 
     } else { 
      grid = (View) convertView; 
     } 

     return grid; 
    } 
} 

Mainactivity.java

 CustomGrid adapter = new CustomGrid(getActivity(), web, imageId); 

grid=(GridView)v.findViewById(R.id.grid); 
grid.setAdapter(adapter); 
     grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            int position, long id) { 
       Toast.makeText(getActivity(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show(); 

      } 
     }); 
+0

あなたのビューに 'xml'ファイルを投稿してください。 – Pztar

+0

@Pztarもう一度私は質問を編集し、ビューを提供 –

+0

私はあなたのアイテムビューの下に答えを投稿して、それを試してみてください。 – Pztar

答えて

1

オーバーレイのレイアウトを取得するには、それは子ビューが互いの上にスタックしますだから、あなたはFrameLayoutを使用することができます。あなたの場合、item_layout.xmlはこのようになります。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/ivBackground" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:src="@drawable/myimage"/> 

    <TextView 
     android:id="@+id/tvOverlayText" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/mytext" 
     android:gravity="center"/> 

</FrameLayout> 
+0

OKテキストがうまくいきました。イメージビューの中心にテキストがありますが、すべてのimageviewsがアライメントされていません。私は使用しているpngのディメンションが違うと思いますか? –

+0

これはイメージの問題であり、レイアウト上の問題ではありません。レイアウトは、ビューの全幅/高さを満たすように設定されています。画像が整列していない場合は、寸法が異なる可能性があります。同じ大きさの画像で試してみてください。 – Pztar

1

XMLファイルには、あまりにも複雑です。これは、単にアクティビティ/フラグメントレイアウトでGridViewを使用することで実現できます。次に、Imageviewとtextviewが1つだけ含まれる別のXMLファイルを膨張させるために、Gridview用のカスタムアダプタクラスが必要になります。あなたがGridViewコントロールを始めるためにこのリンクをたどることができ :https://www.learn2crack.com/2014/01/android-custom-gridview.html

+0

私はレイアウトファイルを編集して、画像ビュー –

+0

はい、を確認するのを簡単にしましたが、問題へのあなたのアプローチは正しくありません。市場で入手可能なすべての単一のアンドロイドデバイスのスクリーンショットのような正確な画面を作成することは本当に難しいでしょう。最良の方法は、GridViewを使用することです。上記のリンクを見てください。あなたの問題を解決します。 –

+0

ok私はそのチュートリアルを使用して何かを構築しました。今、最初のイメージのようにgridviewのレイアウトを達成する方法を教えてくれますか? –