[英]Android percentage width layout
我需要将视图的宽度设置为屏幕宽度的50%,然后将此视图水平居中,同时可能有一个或多个按钮,这些按钮可以显示为附加到屏幕的左侧或右侧。
我正在使用相对布局,这样我就可以放置一个带有权重的线性布局,使我的50%居中,同时将任何按钮放在连接到RL左边或右边的LL顶部。 但是这个布局缺少蓝色中间栏。 如果我将中间视图layout_weight设置为1,我将获得3个相同大小的条形图。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<LinearLayout
android:id="@+id/stupid_android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:layout_weight="1" />
<View
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#0000FF"
android:layout_weight="2" />
<View
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
您应该将视图的宽度设置为0dip
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<LinearLayout
android:id="@+id/stupid_android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#FF0000"
android:layout_weight="1" />
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#0000FF"
android:layout_weight="2" />
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
您可以使用新的百分比库实现此目的:
https://developer.android.com/tools/support-library/features.html#percent
做这样的事情:
<android.support.percent.PercentRelativeLayout 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="48dp">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#FF0000"
app:layout_widthPercent="25%" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#0000FF"
android:centerHorizontal="true"
app:layout_marginLeftPercent="25%"
app:layout_widthPercent="50%" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:alignParentRight="true"
android:background="#00FF00"
app:layout_marginLeftPercent="75%"
app:layout_widthPercent="25%" />
</android.support.percent.PercentRelativeLayout>
使用新的百分比支持库。
compile 'com.android.support:percent:24.0.0'
示例:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.percent.PercentRelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
>
<View
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FF0000"
app:layout_widthPercent="33%" />
<View
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#0000FF"
app:layout_marginLeftPercent="33%"
app:layout_widthPercent="33%" />
<View
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00FF00"
app:layout_marginLeftPercent="66%"
app:layout_widthPercent="33%" />
</android.support.percent.PercentRelativeLayout>
</LinearLayout>
有关Android Doc的更多信息
如果我理解正确,你需要这个:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<LinearLayout
android:id="@+id/stupid_android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:layout_weight="1" ></View>
<View
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#0000FF"
android:layout_weight="1.5" ></View>
<View
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1.5" ></View>
</LinearLayout>
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.