우리는 GridLayout에 조각을 동적으로 추가하려고합니다. 빈 그리드 레이아웃은 XML에서 조각의 레이아웃과 마찬가지로 정의됩니다. 런타임에 우리는 몇 가지 데이터를 조사하여 레이아웃에 추가 할 프래그먼트 수와 각 프래그먼트에 사용할 레이아웃을 결정합니다. 프래그먼트가 생성 된 뷰에 크기를 할당하면 모든 것이 작동하지만, 프래그먼트의 레이아웃 파일에서 크기를 지정하면 그리드 레이아웃에 아무 것도 나타나지 않습니다. 분명히 우리는 뷰를 생성 할 때 단순히 크기를 지정할 수 있지만 각 장치의 올바른 레이아웃을 선택하기 위해 안드로이드 내장 시스템을 활용할 수 있기 때문에 조각에 대한 xml 레이아웃에서이를 수행하는 것을 선호합니다.GridLayout에 xml 레이아웃을 가진 조각을 동적으로 추가하는 작업이 없습니다.
지원 라이브러리 조각을 사용 중입니다. 그 차이를 취하면 나는 지원 라이브러리 GridLayout과를 사용하고 있지 않다
관련 코드 및 XML은 다음과 같습니다
GridLayout과의 XML 다음 조각 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>
예제
<?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>
조각 모음 onCr eateView() 메소드는
@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;
}
나는 그 그것을 커버해야한다고 생각 레이아웃
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;
}
에 조각을 추가 할 수 있습니다. 다시 말해, onCreateView()에서 치수를 명시 적으로 설정하는 선의 주석을 지우면 GridLayout에 올바르게 표시되므로 조각 트랜잭션과 마찬가지로 조각과 해당 작업을 추적하는 모든 것을 알 수 있습니다. 문제는 조각의 XML에서 크기를 지정하려고 시도 할 때 발생합니다.이 경우 빈 화면이 나타납니다.
당신의 생각, 제안 및 근사함은 높이 평가됩니다. 감사합니다. 자레드
사실 우리가 한 일입니다. 나는 당신이 선택한 대답을 줄 것이다. 그러나 당신이 그것을 쓸 때가 있고 명확하게 생각한 후에. – Jared