所以我想要显示GridLayout
,ImageView
&每个单元格中TextView
。每个TextView
都应放在网格单元格中ImageView
的正下方。
问题在于,GridLayout
每次添加只接受一个视图,而GridView
执行作业的Adapter
不同。
请注意,我不能在此使用GridView
,因为限制行数有限制,因此GridView
不适合我。
我想知道是否有任何方法可以创建一个包含两个不同视图的自定义视图。将其添加到GridLayout
。
感谢您的时间!
答案 0 :(得分:1)
是的,您可以创建自定义视图。
在res / layout中创建一个XML布局文件,其中包含视图所需的布局(很可能是带有LinearLayout
和ImageView
的垂直TextView
。
然后为自定义视图创建一个类,使布局膨胀。
public class CustomView extends LinearLayout {
public CustomView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(inflater != null){
inflater.inflate(R.layout.my_layout, this);
}
}
}
然后可以在GridLayout
中访问此内容,如下所示:
<GridLayout>
<com.yourpackage.CustomView android:id="@+id\id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.yourpackage.CustomView>
</GridLayout>
答案 1 :(得分:0)
在我的位置,我会将RecyclerView与GridLayoutManager一起使用。
你可以设置它:
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
其中数字表示行数。 对于RecyclerView中的每个项目,您都可以使用自定义布局(如在所有适配器中),这不需要是单个视图
答案 2 :(得分:0)
你是对的,你可以创建自己的视图。
首先,您需要创建一个看起来像这样的布局文件
my_linear_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:src="@android:drawable/sym_def_app_icon"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
然后,您为此视图创建自己的类并对其进行充气
MyView.java
package com.example.stack.test38526476;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyView extends LinearLayout {
private TextView text;
private ImageView image;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
inflate(getContext(), R.layout.my_linear_layout, this);
this.text = (TextView)findViewById(R.id.textView1);
this.image = (ImageView)findViewById(R.id.imageView1);
}
}
最后,您可以在GridLayout中使用它
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.stack.test38526476.MainActivity">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<com.example.stack.test38526476.MyView/>
<com.example.stack.test38526476.MyView/>
<com.example.stack.test38526476.MyView/>
</GridLayout>
</RelativeLayout>