Android 中 Snackbar 上的 ImageView 不占用最小宽度。

huangapple go评论55阅读模式
英文:

ImageView on snackbar in Android not taking ṁinimum width

问题

我正在尝试启动一个带有关闭图标的“snackbar”来在最后关闭它。这是我的自定义“snackbar”,对默认“snackbar”进行了一些更改。在横向模式下,当设备上有足够的空间来显示文本和图标时,它工作正常。

但是在纵向模式下,通过布局检查器,我可以看到视图在屏幕上。但它没有采用它应该采用的最小宽度。尽管在XML文件中,我已经定义了它应该采用的最小高度和宽度。

可能的原因是什么,我该如何修复它?

我的布局是LinearLayout

我用于布局的XML文件如下:

<?xml version="1.0" encoding="utf-8"?>
<view
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    class="com.example.testtransitentviews.CustomSnackbarContentLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <ImageView
        android:id="@+id/snackbar_image"
        android:src="@drawable/pw_diamond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|start"
        android:minHeight="24dp"
        android:minWidth="24dp"
        app:tint="@color/white" />

    <TextView
        android:id="@+id/snackbar_text"
        style="?attr/snackbarTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="@color/color_snackbar_text" />

    <ImageView
        android:id="@+id/snackbar_action_image"
        android:src="@drawable/ic_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="24dp"
        android:minWidth="24dp"
        android:contentDescription="Close"
        android:layout_gravity="center_vertical|end"/>
</view>

这是我的扩展LinearLayoutCustomSnackbarContentLayout类:

public class CustomSnackbarContentLayout extends LinearLayout implements ContentViewCallback {
    private ImageView iconImageView;
    private TextView messageView;
    private ImageView closeActionView;

    public CustomSnackbarContentLayout(@NonNull Context context) {
        this(context, null);
    }

    public CustomSnackbarContentLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        iconImageView = findViewById(R.id.snackbar_image);
        messageView = findViewById(R.id.snackbar_text);
        closeActionView = findViewById(R.id.snackbar_action_image);
    }

    public ImageView getIconImageView() {
        return iconImageView;
    }

    public TextView getMessageView() {
        return messageView;
    }

    public ImageView getCloseActionView() {
        return closeActionView;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public void animateContentIn(int delay, int duration) {
        messageView.setAlpha(0f);
        messageView.animate().alpha(1f).setDuration(duration).setStartDelay(delay).start();

        if (closeActionView.getVisibility() == VISIBLE) {
            closeActionView.setAlpha(0f);
            closeActionView.animate().alpha(1f).setDuration(duration).setStartDelay(delay).start();
        }
    }

    @Override
    public void animateContentOut(int delay, int duration) {
        messageView.setAlpha(1f);
        messageView.animate().alpha(0f).setDuration(duration).setStartDelay(delay).start();

        if (closeActionView.getVisibility() == VISIBLE) {
            closeActionView.setAlpha(1f);
            closeActionView.animate().alpha(0f).setDuration(duration).setStartDelay(delay).start();
        }
    }
}

这是我使用的“close”按钮的XML文件:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0"><path
    android:pathData="M19,6.4L17.6,5L12,10.6L6.4,5L5,6.4l5.6,5.6L5,17.6L6.4,19l5.6,-5.6l5.6,5.6l1.4,-1.4L13.4,12L19,6.4z"
    android:fillColor="#FFFFFF"/>
</vector>

根据这些信息,即使我将"ImageView"的属性设置为"wrap_content",它应该至少采用24dp的高度和宽度。

如果你能找出为什么我的"snackbar"上的关闭按钮没有采用所需的最小宽度,那么请回答。这将是一次巨大的帮助!

提前感谢。

英文:

I am trying to launch a snackbarwith a close icon to dismiss it at the end. This is my custom snackbarbuilt with some changes over the default snackbar. It's working fine in landscape mode when there is enough space on the device to show the text and icons.

Android 中 Snackbar 上的 ImageView 不占用最小宽度。

But in portrait mode, through layout inspector I can see that the view is there on the screen. But it's not taking the minimum width that it should take. Although in the XML file, I have defined the minimum height and width it should take.

Android 中 Snackbar 上的 ImageView 不占用最小宽度。

What could be the possible reason for this and how can I fix it?

My layout is LinearLayout

And my xml file for the layout is:-

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;view
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
class=&quot;com.example.testtransitentviews.CustomSnackbarContentLayout&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:minHeight=&quot;48dp&quot;&gt;
&lt;ImageView
android:id=&quot;@+id/snackbar_image&quot;
android:src=&quot;@drawable/pw_diamond&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_vertical|start&quot;
android:minHeight=&quot;24dp&quot;
android:minWidth=&quot;24dp&quot;
app:tint=&quot;@color/white&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/snackbar_text&quot;
style=&quot;?attr/snackbarTextViewStyle&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_gravity=&quot;center_vertical&quot;
android:ellipsize=&quot;end&quot;
android:maxLines=&quot;1&quot;
android:textColor=&quot;@color/color_snackbar_text&quot; /&gt;
&lt;ImageView
android:id=&quot;@+id/snackbar_action_image&quot;
android:src=&quot;@drawable/ic_close&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:minHeight=&quot;24dp&quot;
android:minWidth=&quot;24dp&quot;
android:contentDescription=&quot;Close&quot;
android:layout_gravity=&quot;center_vertical|end&quot;/&gt;
&lt;/view&gt;

This is my class CustomSnackbarContentLayout which is extending LinearLayout

public class CustomSnackbarContentLayout extends LinearLayout implements ContentViewCallback {
private ImageView iconImageView;
private TextView messageView;
private ImageView closeActionView;
public CustomSnackbarContentLayout(@NonNull Context context) {
this(context, null);
}
public CustomSnackbarContentLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
iconImageView = findViewById(R.id.snackbar_image);
messageView = findViewById(R.id.snackbar_text);
closeActionView = findViewById(R.id.snackbar_action_image);
}
public ImageView getIconImageView() {
return iconImageView;
}
public TextView getMessageView() {
return messageView;
}
public ImageView getCloseActionView() {
return closeActionView;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public void animateContentIn(int delay, int duration) {
messageView.setAlpha(0f);
messageView.animate().alpha(1f).setDuration(duration).setStartDelay(delay).start();
if (closeActionView.getVisibility() == VISIBLE) {
closeActionView.setAlpha(0f);
closeActionView.animate().alpha(1f).setDuration(duration).setStartDelay(delay).start();
}
}
@Override
public void animateContentOut(int delay, int duration) {
messageView.setAlpha(1f);
messageView.animate().alpha(0f).setDuration(duration).setStartDelay(delay).start();
if (closeActionView.getVisibility() == VISIBLE) {
closeActionView.setAlpha(1f);
closeActionView.animate().alpha(0f).setDuration(duration).setStartDelay(delay).start();
}
}
}

This is my XML file for the drawable "close" button that I am using.

&lt;vector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:width=&quot;24dp&quot;
android:height=&quot;24dp&quot;
android:viewportWidth=&quot;24.0&quot;
android:viewportHeight=&quot;24.0&quot;&gt;&lt;path
android:pathData=&quot;M19,6.4L17.6,5L12,10.6L6.4,5L5,6.4l5.6,5.6L5,17.6L6.4,19l5.6,-5.6l5.6,5.6l1.4,-1.4L13.4,12L19,6.4z&quot;
android:fillColor=&quot;#FFFFFF&quot;/&gt;
&lt;/vector&gt;

As per this also, even if I give wrap content as attribute to my ImageView, it should take at least 24dp height and width.

If you are able to figure out why my close button on snackbar is not taking the required min width, then please answer. It will be a great help!

Thanks in advance.

答案1

得分: 1

你需要硬编码TextView的水平权重,以允许关闭按钮显示:

<TextView
    android:id="@+id/snackbar_text"
    style="?attr/snackbarTextViewStyle"
    android:layout_width="0dp"
    android:layout_weight="1"
    ..../>
英文:

You need to hardcode the horizontal weight of the TextView to allow the close button to show:

&lt;TextView
android:id=&quot;@+id/snackbar_text&quot;
style=&quot;?attr/snackbarTextViewStyle&quot;
android:layout_width=&quot;0dp&quot;
android:layout_weight=&quot;1&quot;
..../&gt;

huangapple
  • 本文由 发表于 2023年2月24日 04:43:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定