0

WebviewAndroid用の簡単なアプリケーションを作成しています。接続がない場合、画面全体をカバーするカスタム トースト メッセージを表示していますが、トースト内にImageView. 問題は、次のコードを使用してトーストを実行しているため、トーストが画面全体を覆っていることです。

toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL, 0, 0);

ImageViewトーストの内側を完全に中央に配置するにはどうすればよいですか? これはImageView、次の名前のトースト レイアウトにありますtoast_custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="horizontal"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo" 
        android:background="@color/white" />
</LinearLayout>

これは、接続がないときにトーストを表示するアクティビティのコードです。

public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
    LayoutInflater inflater = getLayoutInflater();

    View layout = inflater.inflate(R.layout.toast_custom_layout,
            (ViewGroup) findViewById(R.id.toast_layout_root));

    Toast toast = new Toast(getApplicationContext());
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    toast.show();
    toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL, 0, 0);
}

アドバイスをいただければ幸いです。

4

2 に答える 2

2

RelativeLayout代わりにa を使用してみてください

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/logo" 
        android:background="@color/white" />

</RelativeLayout>
于 2013-03-06T20:33:17.360 に答える
0

試す:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:orientation="horizontal"
android:padding="5dp" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logo" 
    android:background="@color/white"
    android:gravity="center" />
 </LinearLayout>

関連する部分はandroid:gravity="center"

于 2013-03-06T20:36:34.173 に答える