Trong ứng dụng của chúng tôi, chúng tôi đang cố gắng thêm động các đoạn vào GridLayout. Bố cục lưới trống được định nghĩa trong XML là bố cục cho đoạn. Vào thời gian chạy, chúng tôi kiểm tra một số dữ liệu và từ đó xác định số lượng phân đoạn để thêm vào bố cục cũng như bố cục để sử dụng cho từng đoạn. Khi chúng ta có phân mảnh gán kích thước cho khung nhìn được tạo ra, nó sẽ hoạt động, tuy nhiên nếu chúng ta chỉ định kích thước trong tệp bố trí cho đoạn không có gì xuất hiện trong bố cục lưới. Rõ ràng chúng ta có thể chỉ định kích thước khi tạo khung nhìn nhưng chúng ta muốn thực hiện nó trong bố trí xml cho các mảnh vì điều đó sẽ cho phép chúng ta tận dụng hệ thống tích hợp của Android để chọn bố cục chính xác cho từng thiết bị.Tự động thêm phân đoạn có bố cục xml vào GridLayout không hoạt động
Tôi đang sử dụng các đoạn thư viện hỗ trợ. Tôi KHÔNG sử dụng hỗ trợ thư viện GridLayout nếu mà làm cho một sự khác biệt
Mã liên quan và xml sau:
Các GridLayout XML:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="@+id/grid_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_fragment"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="8dp"
android:layout_marginTop="?android:attr/actionBarSize"
android:overScrollMode="ifContentScrolls" >
<GridLayout
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alignmentMode="alignMargins"
android:animateLayoutChanges="true"
android:columnCount="3"
android:columnOrderPreserved="true"
android:orientation="horizontal"
android:overScrollMode="ifContentScrolls"
android:rowOrderPreserved="true" >
</GridLayout>
</ScrollView>
</merge>
Một thí dụ về các Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="100dp"
android:alpha="1" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="1.0" />
</RelativeLayout>
The Fragment onCr eateView() Phương pháp
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view;
GridLayout.Spec rowSpec = GridLayout.spec(mRowStart, mRowSpan);
GridLayout.Spec columnSpec;
GridLayout.LayoutParams childParams;
if (large) {;
view = inflater.inflate(R.layout.my_place_large, container, false);
columnSpec = GridLayout.spec(mColumnStart, 2);
childParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
//childParams.width = 200; //If I do this everything works regardless of the layout size
} else {
view = inflater.inflate(R.layout.my_place_small, container, false);
columnSpec = GridLayout.spec(mColumnStart, 1);
childParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
//childParams.width = 100; //If I do this everything works regardless of the layout size
}
childParams.setMargins(0, 0, 0, 0);
//childParams.height = 100; //If I do this everything works regardless of the layout size
view.setLayoutParams(childParams);
view.setId(ID);
return view;
}
Để Thêm Những mảnh vỡ vào Layout
private void populateGrid() {
RelativeLayout gridParent = (RelativeLayout) mParentActivity.findViewById(R.id.locations);
mLocationsGrid = (GridLayout) gridParent.findViewById(R.id.grid);
nColumns = mLocationsGrid.getColumnCount();
mAdapter = new MyAdapter(mContext, this, mResolver); //This is how I keep track of the various fragments depending on my app's state
int nCards = mAdapter.getNumberOfCards();
FragmentManager fragmentManager = mParentActivity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (int i = 0; i < nCards; ++i) {
fragmentTransaction.add(mLocationsGrid.getId(), mAdapter.getFragmentAtIndex(i), String.valueOf(i));
}
fragmentTransaction.commit();
mPopulated = true;
}
Tôi nghĩ rằng nên che nó. Chỉ cần nhắc lại, nếu tôi bỏ ghi chú các dòng đặt rõ ràng thứ nguyên trong onCreateView(), chúng hiển thị đúng trong GridLayout để tôi biết mọi thứ theo dõi các đoạn và các tác phẩm như vậy, cũng như giao dịch phân đoạn. Sự cố xảy ra khi tôi thử và chỉ định kích thước trong xml của đoạn trong trường hợp này tôi nhận được một màn hình trống.
Bạn suy nghĩ, đề xuất và suy ngẫm được đánh giá cao. Cảm ơn, Jared
Đây là thực tế những gì chúng tôi đã làm. Tôi sẽ cung cấp cho bạn câu trả lời được lựa chọn mặc dù bạn đã dành thời gian để viết nó lên, và rõ ràng. – Jared