[英]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.