查看android中的gridView项

时间:2016-04-02 07:28:05

标签: android android-layout gridview

我为gridview创建了一个正常工作的类,我在app的其他部分测试它是否正常工作。自定义布局仅包含我测试的那些部分中的imageView。对于一个活动,我需要同时使用imageView和textView,这里是xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="vertical">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView3"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/khattesevom" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView5"
    android:background="@drawable/textblockbio"
    android:textColor="@color/colorText"
    android:layout_gravity="center_horizontal"
    android:padding="5dp" />
</LinearLayout>

我的班级代码:

public class GridLyrics extends BaseAdapter {
private Context mContext;
private final String[] title;
private final int image[];
public GridLyrics(Context context, String[] t, int[] img)
{
    mContext = context;
    this.title = t;
    this.image = img;
}
@Override
public int getCount() {
    return title.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    if (convertView == null)
    {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_lyrics, parent, false);

        TextView textView = (TextView) grid.findViewById(R.id.textView5);
        ImageView imageView = (ImageView)grid.findViewById(R.id.imageView3);
        textView.setText(title[position]);
        imageView.setImageResource(image[position]);
    }
    else
    {
        grid = (View) convertView;
    }

    return grid;
}
}

以下是我使用它的方式:

    GridView gridView = (GridView) tb2.findViewById(R.id.gridView4);
    String[] t = {"Marham", "Onam Mesle Mane", "Mikham", "Door", "To Besh Migi Mariz", "Oraman", "Test"};
    int[] img = {R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom};
    gridView.setAdapter(new GridLyrics(getContext(), t, img));

我的问题是在textView dosn出现的最后一行!它显示图片但不显示textView。

the picture of app 我不知道自己能做什么!

enter image description here

2 个答案:

答案 0 :(得分:1)

TextView身高设置为 match parent 并删除填充。 像下面一样更改你的xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="vertical">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView3"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/khattesevom" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView5"
    android:background="@drawable/textblockbio"
    android:textColor="@color/colorText"
    android:layout_gravity="center_horizontal"
     />
</LinearLayout>

我认为这对你有用。

答案 1 :(得分:0)

这个怎么样?通过向下滚动,将LinearLayout更改为ScrollView,即可获得他的名字