2013-03-06 18 views
0

我正在爲Android創建一個簡單的Webview應用程序。當沒有連接時,我顯示一個自定義的烤麪包信息,它將覆蓋所有的屏幕,但在烤麪包片中,我想用ImageView顯示圖像。問題是,在吐司覆蓋所有屏幕,因爲我使用下面的代碼來做到這一點:Android中Toast中的ImageView中心

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

怎麼可能我把烤麪包片內ImageView完全居中?這是一個名爲toast_custom_layout.xml敬酒佈局ImageView

<?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); 
} 

我會非常感謝你的建議,朋友。

+1

使用的RelativeLayout及其layout_centerInParent屬性... – yahya 2013-03-06 20:32:54

+0

感謝葉海亞,這個工作整齊! – 2013-03-06 21:44:08

回答

2

嘗試使用RelativeLayout代替

<?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> 
+0

非常感謝@Catherine,''RelativeLayout'與'android:layout_centerInParent =「true」'做了魔術,我非常感謝您的幫助! – 2013-03-06 21:41:52

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"