2016-06-10 105 views
0

我有一個列表視圖,並填充一個自定義適配器。我希望我所有的listview項目都有25%的高度。我試圖使用沒有結果的佈局權重。任何幫助?25%高度的列表視圖項目

這裏是我的代碼

NavigationAdapter

public class NavigationAdapter extends ArrayAdapter<String> { 

    private final Activity context; 
    private final String[] NavigationTexts; 
    private final String[] NavigationImages; 

    public NavigationAdapter(Activity context, String[] NavigationTexts, String[] NavigationImages) { 
     super(context, R.layout.navigation_layout, NavigationTexts); 

     this.context=context; 
     this.NavigationTexts=NavigationTexts; 
     this.NavigationImages=NavigationImages; 
    } 

    public View getView(int position, View view, ViewGroup parent) { 
     LayoutInflater inflater=context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.navigation_layout, parent, false); 

     TextView Name = (TextView) rowView.findViewById(R.id.NavigationText); 
     ImageView ImgButton = (ImageView) rowView.findViewById(R.id.NavigationImage); 


     Name.setText(NavigationTexts[position]); 
     int resID = context.getResources().getIdentifier(NavigationImages[position], "drawable", getContext().getPackageName()); 
     ImgButton.setImageResource(resID); 

     return rowView; 

    } 

} 

和我的佈局文件

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

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="150dp" 
     android:id="@+id/NavigationImage" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:scaleType="fitXY" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/NavigationText" 
     android:layout_alignBottom="@+id/NavigationImage" 
     android:layout_marginBottom="56dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textColor="#ffffff" 
     android:gravity="center" /> 
</RelativeLayout> 
+0

是的,很好。告訴我,你想有4個listview項目,每個項目佔25%的空間?或者你想要別的東西佔25%的空間? – Vucko

回答

0

您可以在customAdapter手動設置。正如你已經覆蓋onCreateViewHolder方法和列表項充氣實際佈局,可以這樣:

僞代碼:

var listViewHeight = context.getResources().getDisplayMetrics().height/4; 
//if you have toolbar or some other component, take its height into calculation also. 
View view = inflater.inflate(layoutId, container, false); 
View listViewContainer = view.findViewById(containerId); 
listViewContainer.getLayoutParams().height = listViewheight; 

UPDATE:

我會建議使用RecyclerView而不是ListView和RecyclerView。適配器,而不是ArrayAdapter。但要回答你的問題:

它的邏輯與上面相同。

將id設置爲父親RelativeLayout爲「layoutContainer」或其他,並在getView()方法中使用上面的行。

+0

看到我的更新問題 – Kinggeov