8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

.xml のレイアウトが正しく表示されない場合の対処方法(Android)

Last updated at Posted at 2015-01-12

1年半前の投稿

LinearLayoutRelativeLayout で囲った部分にパーツを配置している際に「ちゃんと書いているはずなのに正しく表示されない」または「match_parent を設定しているのに画面全体に表示できない」という場合が稀にあります(リスト系ライブラリのセルの .xml レイアウトを作成している場合など)。

そのような場合は LinearLayoutRelativeLayout の外側をもう1つの LinearLayout または RelativeLayout で囲んで2重にしておきます。
基本的にこうしていればレイアウトに関しては無問題です。

:arrow_double_down: 変更前 :arrow_double_down:

<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>

:arrow_double_down: 変更後 :arrow_double_down:

<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 を何階層も入れ子にするとアプリのパフォーマンスが悪くなるのでベストな解決法ではないです :dizzy_face:

GridView を上下にスクロールすると一瞬カクつく現象が発生するようになりました。

これは GridView のライブラリがセルのルートの ViewGroup に干渉して起こった悲劇なのでしょうか?

match_parent を設定しているのに画面全体に表示できない」という場合が稀にあります

これはルートの ViewGroup を RelativeLayout にすると解決できます。

DialogFragment での比較

レイアウトのルートに LinearLayout を使用した場合

dialog.xml
<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>

Screenshot_2016-04-25-21-46-09.png

レイアウトのルートに RelativeLayout を使用した場合

dialog.xml
<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>

Screenshot_2016-04-25-21-40-50.png

共通の処理

SampleDialogFragment.java
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;
    }
}
SampleActivity.java
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");
    }
}
8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?