我将自定义样式应用于我的NavigationView,如下所示:
app:theme="@style/NavigationDrawerStyle"
在我的styles.xml中,像这样:
<style name="NavigationDrawerStyle">
<item name="android:textSize">16sp</item>
<item name="android:listPreferredItemHeightSmall">50dp</item>
<item name="listPreferredItemHeightSmall">50dp</item>
</style>
但是,无论我插入自定义样式的dp值如何,我使用的图标都不会改变大小。它所做的只是缩放每个项目所占用的空间,两个选项之间的空间增加等,但图标大小是固定的。我找不到任何选项/属性来修改图标大小。我怎么做?
这是我正在使用的示例图标:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="73dp"
android:height="93dp"
android:viewportWidth="73.79"
android:viewportHeight="93.25">
<path
android:pathData="M73.06,9.52 L36.9,0 0.74,9.52s-9,67.56 36.16,83.74C82.1,77.08 73.06,9.52 73.06,9.52Z"
android:fillColor="#2980b9"/>
<path
android:pathData="M36.9,0V93.25C-8.3,77.08 0.74,9.52 0.74,9.52Z"
android:fillColor="#3498db"/>
<path
android:pathData="M36.9,44.25m-25.69,0a25.69,25.69 0,1 1,51.38 0a25.69,25.69 0,1 1,-51.38 0"
android:fillColor="#2c3e50"/>
<path
android:pathData="M36.9,23a10,10 0,1 1,-10 10A10,10 0,0 1,36.9 23Z"
android:fillColor="#ecf0f1"/>
<path
android:pathData="M56.78,60.51a25.69,25.69 0,0 1,-39.77 0,21.31 21.31,0 0,1 39.77,0Z"
android:fillColor="#ecf0f1"/>
</vector>
编辑:我还希望能够分别指定高度和宽度,如果可能的话。
我重新格式化了一个问题作者的例子,从android到svg,以试图解释缩放原理。
width="73" height="93" viewBox="0 0 73 93"
width
andheight
是viewport
- 我们在小工具展示中看到的区域。
viewport / viewBox
的比例决定了图标图像的比例。
在我们的例子中,比例将是M1:1
即,图标将显示,73 x 93
当增加viewport
时,让我们说两次width = "146"
height = "186"
比例将是 - viewport / viewBox = 2
图标图像也将加倍。
示例中的视口显示区域是红框。
通过容器样式更改图标大小的尝试由绿框的示例指示。
<style>
#container-Green {
display:block;
width:146;
height:186;
outline: 1px solid green;
}
</style>
<div id="container-Green">
<svg width="73" height="93" viewBox="0 0 73 93" style="border:1px solid red;" >
<g id="icon">
<path d="M73.06,9.52 L36.9,0 0.74,9.52s-9,67.56 36.16,83.74C82.1,77.08 73.06,9.52 73.06,9.52Z"
fill="#2980b9"/>
<path d="M36.9,0V93.25C-8.3,77.08 0.74,9.52 0.74,9.52Z"
fill="#3498db"/>
<path d="M36.9,44.25m-25.69,0a25.69,25.69 0,1 1,51.38 0a25.69,25.69 0,1 1,-51.38 0"
fill="#2c3e50"/>
<path
d="M36.9,23a10,10 0,1 1,-10 10A10,10 0,0 1,36.9 23Z"
fill="#ecf0f1"/>
<path
d="M56.78,60.51a25.69,25.69 0,0 1,-39.77 0,21.31 21.31,0 0,1 39.77,0Z"
fill="#ecf0f1"/>
</g>
</svg>
</div>
结论:
viewport
结论:
viewBox
的值设置为百分比时,图标图像会响应。<style>
#container-Green {
display:block;
width:30%;
height:30%;
outline: 1px solid green;
}
</style>
<div id="container-Green">
<svg width="30%" height="30%" viewBox="0 0 73 93" style="border:1px solid red;" >
<g id="icon">
<path d="M73.06,9.52 L36.9,0 0.74,9.52s-9,67.56 36.16,83.74C82.1,77.08 73.06,9.52 73.06,9.52Z"
fill="#2980b9"/>
<path d="M36.9,0V93.25C-8.3,77.08 0.74,9.52 0.74,9.52Z"
fill="#3498db"/>
<path d="M36.9,44.25m-25.69,0a25.69,25.69 0,1 1,51.38 0a25.69,25.69 0,1 1,-51.38 0"
fill="#2c3e50"/>
<path
d="M36.9,23a10,10 0,1 1,-10 10A10,10 0,0 1,36.9 23Z"
fill="#ecf0f1"/>
<path
d="M56.78,60.51a25.69,25.69 0,0 1,-39.77 0,21.31 21.31,0 0,1 39.77,0Z"
fill="#ecf0f1"/>
</g>
</svg>
</div>
的百分比或父容器中的百分比来更改图标的初始大小。我最喜欢的主题是svg,在android中我理解得更糟。 但在了解了缩放如何工作之后,您可能无法将我的示例从svg重新格式化为android。
很少搜索android中百分比值的主题:
看,可以帮到你:
viewport
要连接库,请将依赖项添加到应用程序的viewport
文件的dependencies部分:
<android.support.percent.PercentFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:background="#f0f0f0"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%" />
</android.support.percent.PercentFrameLayout>
23.0.0 - 库的当前最新版本,可能会随时间而变化。
build.gradle