【发布时间】:2016-03-02 19:26:13
【问题描述】:
我在这里研究了所有相关问题,但仍然无法解决这个问题。 我有一个带有自定义适配器的 GridView。此 GridView 应在两列中显示方形项目。我为项目布局制作了一个 SquareLinearLayout 类:
public class SquareLinearLayout extends LinearLayout {
public SquareLinearLayout(Context context) {
super(context);
}
public SquareLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
GridLayout 适配器在这里膨胀视图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.category_entry, parent, false);
}
if (convertView != null) {
((TextView) convertView.findViewById(R.id.category_name)).setText(getItem(position).Name());
}
return convertView;
}
还有项目:
<xxx.SquareLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:background="@color/colorPrimaryTransparent"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/category_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
</LinearLayout>
</xxx.SquareLinearLayout>
还有我的布局:
<RelativeLayout
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/categories"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:scrollbars="vertical"/>
</RelativeLayout>
启动 GridView 后显示项目 6 全尺寸项目和滚动条拇指。然后拇指消失。而且没有任何滚动条,我无法滚动它。我尝试了 3 列,得到了 15 个没有滚动条的可见项目。 这仅在 Android 4 中发生。Android 5 允许滚动。
我做错了什么?
【问题讨论】: