android如何改变<item>的宽度和高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10706853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android how to change <item> width and height
提问by Asaf Nevo
I'm making a <layer-list>
for drawable.
我正在制作一个<layer-list>
可绘制的。
I have my background image, and I want the second layer to be smaller,
but it seems that doesn't matter what I'm writing inside my android:layer_width
and android:layer_height
.
我有我的背景图像,我希望第二层更小,但似乎我在我的android:layer_width
和android:layer_height
.
The second layer size is still the same.
第二层大小仍然相同。
Here is my xml:
这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item
android:drawable="@drawable/picuser"
android:layout_width="50dp"
android:layout_height="50dp" />
<item
android:drawable="@drawable/ic_launcher"
android:layout_width="10dp"
android:layout_height="10dp" />
</layer-list>
采纳答案by amp
I hope this put you on the right direction:
我希望这能让你走上正确的方向:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/picuser"/>
<item>
<bitmap
android:src="@drawable/ic_launcher"
android:gravity="center" />
</item>
</layer-list>
As you can see here, <item>
doesn't have layout_width
/layout_height
attributes.
正如你在这里看到的,<item>
没有layout_width
/layout_height
属性。
回答by oznus
This is kind of a workaround, but it worked for me.
You can use padding in order to make the second drawable smaller.
这是一种解决方法,但对我有用。
您可以使用填充来缩小第二个可绘制对象。
<item android:drawable="@drawable/circyle" />
<item
android:drawable="@drawable/plus"
android:top="10dp"
android:bottom="10dp"
android:right="10dp"
android:left="10dp" />
回答by Davide Cannizzo
Unlike someone said, there are width and height attributes for <item>
as well. Thanks to @Entreco for posting a demonstration of that.android:width
and android:height
for Drawable
s are similar to android:layout_width
and android:layout_height
for View
s, unlike they can be set to neither match_parent
nor wrap_content
, but you can achieve the same behavior of match_parent
by setting the attribute android:gravity
to either left|right
or start|end
(for matching parent's width), or top|bottom
(for matching parent's height).
不像有人说的,还有宽度和高度属性<item>
。感谢@Entreco 发布了一个演示。android:width
和android:height
for Drawable
s 类似于android:layout_width
和android:layout_height
for View
s,不像它们可以设置为 nonematch_parent
或wrap_content
,但是您可以match_parent
通过将属性设置android:gravity
为left|right
or start|end
(用于匹配父级的宽度)或top|bottom
(用于匹配父级的高度)来实现相同的行为。
Unfortunately, those attributes are available just since API 23.
不幸的是,这些属性在API 23 之后才可用。
However, considering the above method I suggested in place of match_parent
and that android:width
and android:height
attributes are available for <size>
element (which has to be put inside a <shape>
) without any API restriction, you could use a simple workaround:
但是,考虑到上面的方法我在地方的建议match_parent
,并且android:width
和android:height
属性都可以用来<size>
元素(其内要放<shape>
)没有任何API限制,你可以使用一个简单的解决方法:
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<size
android:width="152dp"
android:height="152dp"/>
<solid
android:color="#00FFFFFF"/>
</shape>
</item>
<item>
<bitmap
android:gravity="left|right|top|bottom"
android:src="@mipmap/ic_launcher"/>
</item>
</layer-list>
The above solution takes advantage of having a sized <item>
(with a transparent <shape>
) and an unsized one (with a <bitmap>
) that has android:gravity
set to left|top|right|bottom
for matching both parent's dimensions. So, the real size of the <bitmap>
will be determined by that of the unique sized <item>
in the same parent.
上面的解决方案利用了已设置为匹配两个父级尺寸的已调整大小<item>
(带有透明<shape>
)和未调整大小(带有)的优势。因此, 的实际大小将由同一父级中唯一大小的大小决定。<bitmap>
android:gravity
left|top|right|bottom
<bitmap>
<item>
EDIT:
编辑:
Thanks to @Emil S, who noticed that the above solution won't work as a window background. It only works properly as the background or the foreground of any view. I don't know a particular reason for such strange behavior, but I can guess that at the time which system creates a window with the background specified on android:windowBackground
attribute, no layout is performed as no process is started yet. So, Android will end up on rasterizing the drawable at the screen size and stretch it to fill the whole window, I think. This would explain how that could happen.
感谢@Emil S,他注意到上述解决方案不能用作窗口背景。它只能作为任何视图的背景或前景正常工作。我不知道这种奇怪行为的具体原因,但我可以猜测在哪个系统创建一个具有android:windowBackground
属性指定背景的窗口时,由于尚未启动任何进程,因此不会执行任何布局。因此,我认为,Android 最终会以屏幕尺寸对可绘制对象进行光栅化并将其拉伸以填充整个窗口。这将解释这是如何发生的。
EDIT:
编辑:
@Jo?o Carlos pointed out how my solution won't work (because it would cause a cyclic inheritance) when using an adaptive icon (those icons made by a background and a foreground, which support vector drawables). However, his point is a nonsense, because adaptive icons require API 26: one could use directly android:width
and android:height
on the splash screen or something (They just require API 23).
So, in order to get anything to work in any version of Android, you'll need to differentiate two drawables, the former for API < 23, using the solution I posted, and the latter for API >= 23, using the two attributes as I said at the beginning of my post.
@Jo?o Carlos 指出我的解决方案在使用自适应图标(那些由背景和前景制作的图标,支持矢量可绘制对象)时将不起作用(因为它会导致循环继承)。然而,他的观点是无稽之谈,因为自适应图标需要API 26:一个可以直接使用android:width
和android:height
闪屏或东西(他们只需要在API 23)。
因此,为了在任何版本的 Android 中运行任何东西,您需要区分两个可绘制对象,前者用于API < 23,使用我发布的解决方案,后者用于API >= 23,使用两个属性正如我在帖子开头所说的那样。
回答by Entreco
From API Level 23 and higher, you can actually set the width & height of an item
从 API 级别 23 及更高版本开始,您实际上可以设置项目的宽度和高度
<item
android:drawable="@drawable/splash_logo"
android:height="100dp"
android:width="100dp"/>
NOTE:Use android:width
and android:height
instead of android:layout_width
and android:layout_height
注意:使用android:width
andandroid:height
代替android:layout_width
andandroid:layout_height
回答by Jupiter
Try this:
尝试这个:
<?xml version="1.0" encoding="utf-8"?>
<!-- The android:opacity=”opaque” line?is critical in preventing a flash
of black as your theme transitions. -->
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<!-- The background color, preferably the same as your normal theme -->
<item android:drawable="@android:color/white" />
<item
android:height="100dp"
android:width="100dp"
android:gravity="center">
<bitmap
android:src="@drawable/ic_launcher_bc_512"/>
</item>
</layer-list>
回答by DàChún
I've tried a lot, but I couldn't find a reliable way to center the logo within an XML layout. Finally, I found the following workaround:
我已经尝试了很多,但我找不到一种可靠的方法来在 XML 布局中居中徽标。最后,我找到了以下解决方法:
mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ImageView imageView = (ImageView)mToolbar.findViewById(R.id.logo);
if (mToolbar == null)
return;
int toolbarWidth = mToolbar.getWidth();
int imageWidth = imageView.getWidth();
imageView.setX((toolbarWidth - imageWidth) / 2);
}
});
layout_toolbar.xml:
layout_toolbar.xml:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
style="@style/Toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="@drawable/toolbar_background"
android:focusable="false"
app:titleTextAppearance="@style/AppTheme.Toolbar.Title">
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/toolbar_logo" />
</android.support.v7.widget.Toolbar>