比较来自世界各地的卖家的域名和 IT 服务价格

Android Drawable: 在文件中以百分比指定图形的宽度 XML?

我正在尝试创建一个简单的绘图对象,我想作为演示的背景安装 /使用

setBackgroundDrawable

/., 我只是想划分绘制对象的背景 2 等于矩形 /50% - 50%/,, 第一个应该填充黑色,二白:


xml version="1.0" encoding="utf-8"?
<layer-list xmlns:android="[url=http://schemas.android.com/apk/res/android">]http://schemas.android.com/apk ... gt%3B[/url]
<item android:id="@+id/back_color_1">
<shape android:shape="rectangle">
<solid android:color="#000000"></solid>
</shape>
</item>
<item android:id="@+id/back_color_2">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF"></solid>
</shape>
</item>
</layer-list>


如何指定每个数字应该具有宽度 50% 在定义文件中 drawable XML? 有点

android:width="50%"

.

/我工作NAD. Android 3.0, 但我认为这是一般问题 Android./

P.S: 你可以做到 CSS 或者 XAML.
已邀请:

裸奔

赞同来自:

您无法指定百分比

. 您需要指定它的测量值。


android:width

定义在此:
http://developer.android.com/r ... width
:

/斜体mo./

必须有测量值

, 这是添加到测量单位的浮点数,例如"14.5sp". 可用测量单位: px /像素/, dp /独立像素密度/, sp /基于优选字体大小的可缩放像素/, in /英寸/, mm /毫米/.

有关每个测量类型的定义,请参阅:
http://developer.android.com/g ... nsion
您需要创建自己的属性 /厘米。
http://developer.android.com/r ... .html
/ 然后自己进行计算
onDraw/.../

.

查看这两个问题并为例子链接。

:

https://coderoad.ru/3441396/

https://coderoad.ru/6020379/

石油百科

赞同来自:

这里没有真正的兴趣环境,但你可以接近。

这似乎是实现这一目标的最简单方法 - 使用图像作为绘图背景,只是制作一个黑色和白色分裂图像。

我知道的唯一其他方式分享任何东西 - 它被使用了 2 演示文稿,每个都有自己的颜色 bg, 并且每个都给出了该属性的相同的正值 android:layout_weight /它是。 50/50/. 然后它们将分开可用空间。

希望它会有所帮助!

诸葛浮云

赞同来自:

android:layout_weight= ".20" - 这是实现百分比的最佳方法。


xml version="1.0" encoding="utf-8"?
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="[url=http://schemas.android.com/apk/res/android">]http://schemas.android.com/apk ... gt%3B[/url]
<textview android:id="@+id/logTextBox" android:layout_height="0dp" android:layout_weight=".20" android:layout_width="fill_parent" android:maxlines="500" android:scrollbars="vertical" android:singleline="false" android:text="@string/logText">
</textview>
</linearlayout>

八刀丁二

赞同来自:

我找到了通过创建自己的类继承来执行此操作的方法 RelativeLayout, 我重新定义方法的地方 onMeasure. 在此阶段,已知物种的高度,因此我提取了背景并手动设置元件的插入
http://developer.android.com/r ... 28int,%20int,%20int,%20int,%20int%29
.


LayerDrawable bg = /LayerDrawable/getBackground//;
int h = getMeasuredHeight//;
bg.setLayerInset/1, 0, 0, 0, h/2/;


然而,这种方法有点繁琐。 在大多数情况下,克里斯提供的响应应该足够了。

奔跑吧少年

赞同来自:

正如Svapnil所说,最好的方式, - 它被使用了 layout_weight 在 a LinearLayout.
如果您水平进行,请安装 layout_width 在 "fill_parent", 然后安装 layout_weight 您需要的百分比。 从以下类似的东西开始,并使用价值观 layout_width, 了解它的想法 /或者添加更多表示只是为了更好地理解。/.


<linearlayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal">
<view android:background="#aaffaa" android:layout_height="30dip" android:layout_weight="0.5" android:layout_width="fill_parent"></view>
<view android:background="#aaaaff" android:layout_height="30dip" android:layout_weight="0.5" android:layout_width="fill_parent"></view>
</linearlayout>

要回复问题请先登录注册