我在我的应用中使用了Master / Detail流程。 ItemDetailFragment膨胀test_grid.xml。因为我想让视图完全符合片段,所以我在onViewCreated中插入了一个OnGlobalLayoutListener()来测量我的frag并计算视图的完美大小。到目前为止一切顺利,但来自R.layout的test_grid.xml被默认的视图大小膨胀,然后突然膨胀我的计算尺寸,这当然不是我计划发生的。
我在onViewCreated()中为我的rootView和Gridlayout尝试了View.Gone,但是无法使其获得可见的增益。所有这些麻烦只是为了防止以编程方式创建整个布局,其中包含42个单元格。这似乎是更好的方式,除了我无法通过这个问题。有什么建议吗?
test_grid.xml:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_test_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="6"
android:orientation="horizontal"
android:rowCount="7"
android:visibility="visible"
>
<Space
android:id="@+id/space_6"
android:layout_width="0dp"
android:layout_height="0dp" />
<TextView
android:id="@+id/tv_name"
android:layout_gravity="fill"
android:gravity="center_vertical|center_horizontal"
/>
<EditText
android:id="@+id/et_name"
android:layout_gravity="fill"
android:gravity="center_vertical|center_horizontal"
/>
... more views...
<Space
android:id="@+id/space_35"
android:layout_width="0dp" />
</GridLayout>
ItemDetailFragment.java: package com.example.ccc;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.Toast;
public class ItemDetailFragment extends Fragment {
int fragmentWidth;
int fragmentHeigth;
int bestRowHeight;
int bestSpaceWidth;
int bestViewWidth;
GridLayout mGrid;
int view_show;
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.test_grid, container,
false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
final View rootView = getActivity().findViewById(R.id.my_test_grid);
// mGrid = (GridLayout) rootView.findViewById(R.id.my_test_grid);
// mGrid.setVisibility(View.GONE);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
public void onGlobalLayout() {
rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
fragmentHeigth = rootView.getHeight();
fragmentWidth = rootView.getWidth();
bestRowHeight = fragmentHeigth / 7;
bestSpaceWidth = (fragmentWidth / 100) * 10;
bestViewWidth = (fragmentWidth / 100) * 20;
// change views here!!!
View sp6 = rootView.findViewById(R.id.space_6);
((GridLayout) rootView).removeView(sp6);
View sp35 = rootView.findViewById(R.id.space_35);
((GridLayout) rootView).removeView(sp35);
setRowsHeight(sp6, bestRowHeight);
setColsWidth(sp35, bestSpaceWidth);
((GridLayout) rootView).addView(sp6, 5);
// mGrid = (GridLayout)
// rootView.findViewById(R.id.my_test_grid);
// mGrid.setVisibility(View.VISIBLE);
}
});
super.onViewCreated(view, savedInstanceState);
// mGrid = (GridLayout) rootView.findViewById(R.id.my_test_grid);
// mGrid.setVisibility(View.VISIBLE);
}
private void setColsWidth(View v, int width) {
LayoutParams lp;
lp = v.getLayoutParams();
lp.width = width;
v.setLayoutParams(lp);
}
private void setRowsHeight(View v, int height) {
LayoutParams lp;
lp = v.getLayoutParams();
lp.height = height;
v.setLayoutParams(lp);
}
public void setWidthHeight(View v, int width, int height) {
LayoutParams lp;
lp = v.getLayoutParams();
lp.width = width;
lp.height = height;
v.setLayoutParams(lp);
}
}
正如您所看到的,我试图让视图在代码的不同位置可见或消失,但没有一个是可行的解决方案。