2012-03-23 1 views
0

私はscrollview内にGridViewを持っており、GridViewには4つの画像が(ImageAdapter経由で)ロードされています。私の問題は、私は3枚の画像を見せることができますが、4枚目は表示できません。さらに調査したところ、gridviewの高さは行の高さに過ぎないことがわかったので、xmlのgridviewの高さを700dpに設定すると、それが表示されます。ここでAndroid - GridViewの高さを最大にする

はスクリーンショットです:

enter image description here

あなたが見ることができるように、私は、GridViewコントロールがどこにあるか示すためにHotPinkにGridViewコントロールの背景を設定しました。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/llLayoutContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" >  

    <!-- <LinearLayout android:id="@+id/llMiddle" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal"> --> 

     <LinearLayout 
      android:id="@+id/llLeft" 
      android:layout_width="400dp" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:background="@drawable/layout_left" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp"> 
      <ScrollView 
       android:id="@+id/svMenu" 
       android:layout_width="400dp" 
       android:layout_height="match_parent" 
       > 
      </ScrollView>   
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/llRight" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="vertical" 
      android:background="@drawable/layout_right" 
      android:padding="0dp"> 
     </LinearLayout> 
    <!-- </LinearLayout> --> 
</LinearLayout> 

のLinearLayout "llLeftは"(ScrollView内)のメニュー項目がロードされるレイアウトです:

はここMAIN_MENUのためのXMLです。 layout_leftは緑の背景の周りに暗いアウトラインを描画する唯一のシェイプです。ここで

は、GridViewコントロールが含まれていmain_menu_header.xmlです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingTop="5dp"> 

    <TextView 
      android:id="@+id/tvDashboardHeader" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="Dashboard Main Menu" 
      android:textSize="20dp" 
      style="@style/TextShadow" 
      android:paddingTop="5dp" 
      android:gravity="left" 
      android:background="@drawable/menu_item_bg"   
      /> 
    <GridView 
     android:id="@+id/gvMenuItems" 
     android:layout_width="400dp" 
     android:layout_height="match_parent" 
     android:columnWidth="110dp" 
     android:numColumns="auto_fit"  
     android:verticalSpacing="10dp"  
     android:horizontalSpacing="10dp"  
     android:stretchMode="columnWidth" 
     android:background="@color/HotPink" 
     android:gravity="center" > 
    </GridView> 

</LinearLayout> 

そして、ここでは、私がGridViewコントロールを移入方法は次のとおりです。

 ScrollView sv = (ScrollView)findViewById(R.id.svMenu); 
     LayoutInflater liInflater = getLayoutInflater(); 
     ViewGroup vg = (ViewGroup)liInflater.inflate(R.layout.main_menu_header, sv, false); 
     sv.addView(vg); 

     //Setup the Main Menu items on the left side. 
     GridView gvMenuItems = (GridView) findViewById(R.id.gvMenuItems);  
     gvMenuItems.setAdapter(new ImageAdapter(this)); 

ImageAdapterクラス:

ImageAdapterクラス:

public class ImageAdapter extends BaseAdapter { 
    private Context mContext;  

    public ImageAdapter(Context c) { 
     mContext = c;  
    }  

    public int getCount() {   
     return mThumbIds.length;  
    }  

    public Object getItem(int position) {   
     return null;  
    }  

    public long getItemId(int position) {   
     return 0;  
    }  

    // create a new ImageView for each item referenced by the Adapter  
    public View getView(int position, View convertView, ViewGroup parent) {   

     ImageView imageView; 

     if (convertView == null) { 
      // if it's not recycled, initialize some attributes    
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(parent.getLayoutParams().width, 125)); //new GridView.LayoutParams(400, 125));    
      //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
      imageView.setMaxHeight(50); 
      imageView.setMaxWidth(50); 
      //imageView.setPadding(30, 0, 0, 0); 
      //imageView.setPadding(40, 30, 20, 30); 

      } else {    
       imageView = (ImageView) convertView;   
      }   

     imageView.setImageResource(mThumbIds[position]); 

     return imageView; 
    }  

    // references to our images  
    private Integer[] mThumbIds = { 
      R.drawable.home, 
      R.drawable.open_folder, 
      R.drawable.paper_pen, 
      R.drawable.person 
    }; 
} 

私はandroid:layout_height = "match_parent"のgridviewプロパティはうまくいくと思っていましたが、そうではありません。どのように私は、全体の左側を取るために、GridViewを取得することができます任意のアイデア、スクロールビュー内?

答えて

1

私の最高の推測は、scrollviewが問題を与えているということです。

なぜscrollviewが必要ですか?

グリッドビューはスクロールを処理します。それは一種の冗長です。それを削除し、直接膨らんだビューを直接llLeftに追加してください。それが問題を解決します。


実際、xmlを使用してUIの問題を解決するほうが、より洗練されたものになります。 main_menu.xml<include>タグを使用してください。ここであなたは正しい方法

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/llLayoutContainer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" >  

<!-- <LinearLayout android:id="@+id/llMiddle" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:orientation="horizontal"> --> 

    <LinearLayout 
     android:id="@+id/llLeft" 
     android:layout_width="400dp" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:background="@drawable/layout_left" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp"> 
     <include 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     layout="@layout/main_menu_header" />   
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/llRight" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:background="@drawable/layout_right" 
     android:padding="0dp"> 
    </LinearLayout> 
<!-- </LinearLayout> --> 

+0

です... scrollviewが冗長である.... XMLへの変更を行った後、私はちょうどImageAdapterにGridViewのアダプタを設定するのですか?私はこれを試みたが、何も出現していない、ImageAdapterはビューを膨らませないので、私は推測しているが、あまり確かではない。私の脳は少しゴミ箱のATMです。どのようにビューを直接膨張させるのですか? – Robert

+0

私が提案したxmlに従っている場合は、最後の2行のコードを質問に使用するだけで済みます。 //左側のメインメニュー項目を設定します。 GridView gvMenuItems =(GridView)findViewById(R.id.gvMenuItems); gvMenuItems.setAdapter(new ImageAdapter(this)); – PH7

+0

古い方法で試すことができます。スクロールビューを削除するだけです。それもうまくいくはずです。\t GridView gvMenuItems =(GridView)findViewById(R.id.gvMenuItems); – PH7

関連する問題