[英]ANDROID: create a view like a dataGrid but made with LinearLayouts instead
我遇到了一个问题,我无法将我的dataGridView放到scrollView中,并且如果我有很多列,它们只会变得很薄,以致无法在那里看到任何东西。 这就是为什么我决定重新制作它并为每列创建带有Vertical Layout的 LinearLayout,并且它们中的每一个都将具有另一个带有Horizontal Layout的 LinearLayout只是为了模拟GridView。 (希望这是个好主意)
但是很不幸,我在创建过程中遇到了一些问题。 没有创建它,我的应用程序关闭了。 寻求您的帮助
这是我的代码:
grid_container.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">
<ScrollView
android:id="@+id/GridScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/main_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
PageFragment.java(应填写LinearLayout的位置)
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.grid_container, container, false);
LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.main_grid_layout);
int colCount = mPage.get(0).split(",").length;
int rowCount = mPage.size();
ArrayList<ArrayList<String>> tempNormList = createNormList(mPage);
for(int currCol=0;currCol<colCount;currCol++){
LinearLayout linearLayout = new LinearLayout(view.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(llParams);
for(int currRow=0; currRow<rowCount;rowCount++){
TextView textView = new TextView(view.getContext());
textView.setText(tempNormList.get(currCol).get(currRow));
if(currRow==0){
textView.setBackgroundResource(R.drawable.header_borders);
}
linearLayout.addView(textView);
}
mainLayout.addView(linearLayout);
}
return view;
}
预先感谢您的帮助
试试我链接的答案,它将为您提供解决方案,但您需要进行如下更改
getView()
方法for
循环以设置数据。 通过`findViewById() Linearlayout
id为main_grid_layout
。
将getView()
方法的convertView添加到main_grid_layout
希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.