【问题标题】:Centered ImageView inside a Toast in AndroidAndroid中Toast中的居中ImageView
【发布时间】:2013-03-07 04:36:34
【问题描述】:

我正在为 Android 制作一个简单的Webview 应用程序。当没有连接时,我会显示一条自定义的 toast 消息,它将覆盖整个屏幕,但在 toast 内部我想显示带有ImageView 的图像。问题是吐司覆盖了整个屏幕,因为我使用以下代码来执行此操作:

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

如何将ImageView 完全居中放置在吐司中?这是名为 toast_custom_layout.xml 的 toast 布局中的 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);
}

朋友们,我非常感谢您的建议。

【问题讨论】:

  • 使用RelativeLayout及其layout_centerInParent属性...
  • 谢谢 yahya,这很好用!

标签: android android-layout alignment android-imageview toast


【解决方案1】:

尝试改用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>

【讨论】:

  • 非常感谢 @Catherine,RelativeLayoutandroid:layout_centerInParent="true" 创造了奇迹,非常感谢您的帮助!
【解决方案2】:

试试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多