2017-03-02 108 views
0

我想在我的一個活動中顯示用戶在gridview中發佈的所有圖片。這些是我的代碼,我在下面的圖片中顯示結果。我在gridview(90dp,90dp)中指定了每個圖片的寬度和高度,但它並沒有遵循。他們被填充爲他們上傳的大小(橫向/縱向)。甚至在將gridview的高度設置爲wrap_content之後,高度仍然像圖片一樣(大約200dp或者b)。我該如何解決這個問題?我在哪裏做錯了?每張照片的需要所有在gridview中相同大小的圖片android

型號:

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

    <ImageView 
     android:layout_width="90dp" 
     android:layout_height="90dp" 
     app:srcCompat="@drawable/post2" 
     android:id="@+id/imageView2" 
     android:scaleType="centerCrop" 
     android:layout_margin="0dp"/> 


</RelativeLayout> 

我的另一個動作的GridView:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_competition_user" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="8dp" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.golstars.www.glostars.competitionUser" 
    tools:showIn="@layout/activity_competition_user"> 

     <GridView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:columnWidth="90dp" 
      android:numColumns="3" 
      android:verticalSpacing="8dp" 
      android:stretchMode="columnWidth" 
      android:horizontalSpacing="8dp" 
      android:id="@+id/competitionusergrid" 
      /> 



</ScrollView> 

我的適配器:

MyUser mUser = MyUser.getmUser(); 
     mUser.setContext(this); 
     Picasso.with(this).load(mUser.getProfilePicURL()).into(profileFAB); 

     targetList = new ArrayList<>(); 

     targetAdapter = new GridAdapter(getApplicationContext(), targetList); 

     competitionusergrid.setAdapter(targetAdapter); 

     String target = ""; 
     target = this.getIntent().getStringExtra("LOAD_TARGET"); 
     System.out.println(target); 

     if(target.equals("COMPETITION")){ 
      //if we have competition pics 
      if(targetList.isEmpty()){ 
       ArrayList<String> aux = this.getIntent().getStringArrayListExtra("COMPETITION_PICS"); 
       System.out.println("target list - " + aux); 
       for(int i = 0; i < aux.size(); i++){ 
        targetList.add(aux.get(i)); 
        targetAdapter.notifyDataSetChanged(); 
       } 


      } 

如何看起來:UI

回答

0

使用GridView Setup所有圖像都是相同的寬度&高度。

+0

感謝很多人!這正是我想要的觀點。 –

0

我們e RecyclerView而不是GridView,RecyclerView與GridView &一樣易於實現。

enter image description here

+0

能否請您分享UI圖像如何看起來? – rahul

+0

@rahul對不起忘了添加UI,我只是請檢查。 –

+0

@shakilMahmudShanto你不需要爲你使用RelativeLayout單一視圖「每個圖片的模型」,這是你在那裏使用的一個額外的不必要的視圖。只使用ImageView並查看是否導致問題。由於您將圖片視圖設爲90dp,但您的相對佈局爲match_parent。讓我知道如果這不起作用 – rahul

0

如果您想使用當前的代碼,那麼你可以使用以下的ImageView,

public class SquareImageView extends ImageView { 
public SquareImageView(Context context) { 
    super(context); 
} 

public SquareImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

@Override 
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, widthMeasureSpec); 
}} 

而且,每個項目的佈局,

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

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:srcCompat="@drawable/post2" 
     android:id="@+id/imageView2" 
     android:scaleType="centerCrop" 
     android:layout_margin="0dp"/> 


</RelativeLayout> 
相關問題