英文:
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>
这是我的扩展LinearLayout
的CustomSnackbarContentLayout
类:
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 snackbar
with a close icon to dismiss it at the end. This is my custom snackbar
built 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.
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.
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:-
<?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>
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.
<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>
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:
<TextView
android:id="@+id/snackbar_text"
style="?attr/snackbarTextViewStyle"
android:layout_width="0dp"
android:layout_weight="1"
..../>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论