【问题标题】:Android Custom layout for Notification - Issue with Image & text通知的Android自定义布局-图像和文本问题
【发布时间】:2016-03-31 06:38:33
【问题描述】:

我正在尝试为通知构建自定义布局,但面临布局问题。图像显示在中间而不是左侧,文本不可见。尝试过 Liner 布局、文本样式和颜色等,但没有运气。布局中缺少什么?

Custome_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp" >
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp" />
    <TextView android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        style="Custom Notification Title" />
    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/image"
        android:layout_below="@id/title"
        style="Custom Notification Text" />
</RelativeLayout>

Java 代码

private void sendNotification(String title, String msg, String type, String imageURl, String fullid, String alert_price, int count) {
        WakeLocker.acquire(this);
        //msg = msg+"Amit this is testing for long text";
        long when = System.currentTimeMillis();
        //count = count+5;
        System.out.println(msg);
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);


        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this).setSmallIcon(R.drawable.logo);

        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
        contentView.setImageViewResource(R.id.image, R.drawable.logo);
        contentView.setTextViewText(R.id.title, "Amit notification");
        contentView.setTextViewText(R.id.text, msg);
        mBuilder.setContent(contentView);

        Notification notification = mBuilder.build();

        Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.sound);
        mNotificationManager.notify((int) when, notification);
        //RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION,     uri);
        Ringtone ring = RingtoneManager.getRingtone(getApplicationContext(), uri);
        ring.play();
        ((Vibrator) getApplicationContext().getSystemService( Context.VIBRATOR_SERVICE)).vibrate(1600);

        WakeLocker.release();
    }

【问题讨论】:

    标签: android android-layout android-notifications


    【解决方案1】:

    试试这个。

    您在定义@id 时可能会遇到问题,请改用@+id

    只需在您的 XML 中替换以下代码。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dp">
    
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_marginRight="10dp"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher" />
    
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/image"
            android:text="@string/app_name"
            android:textSize="16sp" />
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/title"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/image"
            android:text="@string/app_name"
            android:textSize="16sp" />
    </RelativeLayout>
    

    完成了。

    希望这会对你有所帮助。

    【讨论】:

    • 布局仍然与屏幕截图中显示的相同。图片出现在中间,标题和文字根本不显示。
    • @Amit 我检查了布局是否按预期工作,图像在左侧,两个文本视图都正确显示文本。请在您的 xml 中替换我的代码。
    • Jay,我按原样复制了你的 xml,并用不同的 android 版本和型号尝试了很多次,但它对我不起作用......你是否也使用了我的 java 代码,认为可能有问题java代码..
    • @Amit 那么我的回答有什么问题,他们的建议是有效的。是你图片大小的问题。因此,为此我也给出了ic_launcher 在我的回答中。如果对您有用,请采纳答案。谢谢:)。
    【解决方案2】:

    这是我使用的徽标图像大小的问题。使用 75x75 的较小图像,它与我的布局代码一样工作。 这是一个错误,所以我没有选择这个作为答案,但它可能对未来的人们有用......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多