アプリでマスター/詳細フローを使用しています。ItemDetailFragment は、test_grid.xml を拡張します。ビューをフラグメントに正確に合わせたかったので、onViewCreated に OnGlobalLayoutListener() を挿入してフラグメントを測定し、ビューの最適なサイズを計算しました。これまでのところうまくいきましたが、R.layout の test_grid.xml はデフォルトのビュー サイズで膨らみ、計算されたサイズを突然膨らませます。これはもちろん、私が計画していたものではありません。
rootView と Gridlayout の onViewCreated() で 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: パッケージ 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);
}
}
ご覧のとおり、コードのさまざまな場所でビューを表示または非表示にしようとしましたが、実行可能な解決策はありませんでした。