1年半前の投稿
LinearLayout
や RelativeLayout
で囲った部分にパーツを配置している際に「ちゃんと書いているはずなのに正しく表示されない」または「match_parent
を設定しているのに画面全体に表示できない」という場合が稀にあります(リスト系ライブラリのセルの .xml レイアウトを作成している場合など)。
そのような場合は LinearLayout
や RelativeLayout
の外側をもう1つの LinearLayout
または RelativeLayout
で囲んで2重にしておきます。
基本的にこうしていればレイアウトに関しては無問題です。
変更前
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ボタン①" />
</LinearLayout>
変更後
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ボタン①" />
</LinearLayout>
</RelativeLayout>
実際にあった例としてヘッダーとフッターを追加することのできる GridView ライブラリを使用していた際、ヘッダーに設定する .xml の xmlns:android="http://schemas.android.com/apk/res/android
を定義している1番外側の ViewGroup に layout_margin="10dp"
を設定すると GridView を上下にスクロールすると一瞬カクつく現象が発生するようになりました。
そこで1番外側の ViewGroup に設定している layout_margin="10dp"
を1つ内側に新しい ViewGroup を作成して、そこに移動することでカクつく現象が発生しなくなりました。
何はともあれ、1番外側の ViewGroup には何も設定しない方が無難なようです。
投稿から1年半後の追記
ViewGroup を何階層も入れ子にするとアプリのパフォーマンスが悪くなるのでベストな解決法ではないです 。
GridView を上下にスクロールすると一瞬カクつく現象が発生するようになりました。
これは GridView のライブラリがセルのルートの ViewGroup に干渉して起こった悲劇なのでしょうか?
「
match_parent
を設定しているのに画面全体に表示できない」という場合が稀にあります
これはルートの ViewGroup を RelativeLayout
にすると解決できます。
DialogFragment での比較
レイアウトのルートに LinearLayout を使用した場合
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ボタン①" />
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ボタン②" />
</LinearLayout>
レイアウトのルートに RelativeLayout を使用した場合
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatButton
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ボタン①" />
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:text="ボタン②" />
</RelativeLayout>
共通の処理
import android.support.v4.app.DialogFragment;
public class SampleDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog);
return dialog;
}
}
public class SampleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_activity);
SampleDialogFragment sampleDialogFragment = new SampleDialogFragment();
sampleDialogFragment.show(getSupportFragmentManager(), "SAMPLE_TAG");
}
}