gpt4 book ai didi

android - 如何让customAdapter填充gridview高度

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:10 26 4
gpt4 key购买 nike

我正在开发数独应用程序。布局为 9x9 GridView。每个 GridView 包含 9 个由 customAdapter 提供的 Textview。我想让 9 个 TextView 填充每个 GridView 的高度。怎么办?

这就是我现在拥有的enter image description here

这是 myGridAdapter:

public class myGridAdapter extends BaseAdapter {

private Context context;
private String[] mobileValues;

public myGridAdapter(MainActivity mainActivity, String[] arrayEmpty) {
this.context = mainActivity;
this.mobileValues = arrayEmpty;
}

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

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View gridView;

if (convertView == null) {

gridView = new View(context);

// get layout from text_item.xml
gridView = inflater.inflate(R.layout.text_item, null);

// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);

} else {
gridView = (View) convertView;
}

return gridView;
}
.....
}

这是每个文本项的 XML

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

<TextView
android:id="@+id/grid_item_label"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@drawable/cell_shape"
android:text="@string/_1"
tools:ignore="InefficientWeight"
android:textSize="12sp" >
</TextView>

</LinearLayout>

这是每个 TextView 的形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#777777" />
</shape>

我希望我的 Gridview 像这样 enter image description here

应用 bakriOnFire 的第一个答案后

enter image description here

最佳答案

您可以将在 GridView 的适配器中膨胀的 TextView 的每个 LinearLayout 的高度设置为 screenHeight/3。

在 textView 的 xml 膨胀后的 getView 中,将膨胀后的 View 转换为 LinearLayout 并将其高度设置为:

LayoutInflater li = getLayoutInflater();
LinearLayout ll = (LinearLayout) li.inflate(R.layout.menu_items, null);

int screenHeight = ((Activity) context).getWindowManager()
.getDefaultDisplay().getHeight();

ll.setMinimumHeight(screenHeight/3);

编辑

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent">

text_item.xml

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

<TextView
android:id="@+id/grid_item_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
tools:ignore="InefficientWeight"
android:padding="0dp"
android:textSize="12sp" >
</TextView>

</LinearLayout>

主 Activity .java

public class MainActivity extends Activity {

String[] str = { "1", "2", "3", "1", "2", "3", "1", "2", "3" ,
"1", "2", "3", "1", "2", "3", "1", "2", "3" ,
"1", "2", "3", "1", "2", "3", "1", "2", "3" ,
"1", "2", "3", "1", "2", "3", "1", "2", "3" ,
"1", "2", "3", "1", "2", "3", "1", "2", "3" ,
"1", "2", "3", "1", "2", "3", "1", "2", "3" ,
"1", "2", "3", "1", "2", "3", "1", "2", "3" ,
"1", "2", "3", "1", "2", "3", "1", "2", "3" ,
"1", "2", "3", "1", "2", "3", "1", "2", "3" };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

GridView gridView = (GridView) findViewById(R.id.gridView1);

gridView.setAdapter(new myGridAdapter(this, str));

gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {

}

});

}

public class myGridAdapter extends BaseAdapter {

private Context context;
private String[] mobileValues;

public myGridAdapter(MainActivity mainActivity, String[] arrayEmpty) {
this.context = mainActivity;
this.mobileValues = arrayEmpty;
}

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

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LinearLayout gridView;

if (convertView == null) {

// get layout from text_item.xml
gridView = (LinearLayout)inflater.inflate(R.layout.text_item, null);

int screenHeight = ((Activity) context).getWindowManager()
.getDefaultDisplay().getHeight();

gridView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.MATCH_PARENT, screenHeight/9));


//gridView.setMinimumHeight(screenHeight/9);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);

} else {
gridView = (LinearLayout) convertView;
}

return gridView;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mobileValues.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}

}

在 getView 中我所做的是将 text_item.xml 膨胀 9x9 次,其中我动态地将 text_item 的布局高度设置为 screenHeight/9。让我知道它是否适合您。

编辑2

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:numColumns="9"
android:padding="0dp"
android:background="#000000"
android:horizontalSpacing="3dp"
android:verticalSpacing="3dp"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>

text_item.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#FFFFFF"
android:orientation="vertical" >

<TextView
android:id="@+id/grid_item_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
tools:ignore="InefficientWeight"

android:padding="0dp"
android:textSize="12sp" >
</TextView>

</LinearLayout>

关于android - 如何让customAdapter填充gridview高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16398318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com