我是android开发的新手。我想设置保证金和在Xml中填充,而不是在dp或px中,但以百分比表示。有没有办法做到这一点?
答案 0 :(得分:5)
这是不可能的,尽管你最好通过在java代码中获取如下屏幕的宽度和高度来实现它,并且
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
然后根据屏幕尺寸计算边距,并按
设置 setmargin(int x) //method of view/layout.
通过这种方式,您可以根据屏幕大小设置边距,这对于所有屏幕都不是固定的,基本上就像设置百分比而是来自Java代码。
答案 1 :(得分:0)
如果您尝试以百分比形式分享控件之间的屏幕宽度。在android中执行此操作的最佳方法是使用weight
属性。请查看以下URL以获取更多详细信息。
的问候, Aqif Hamid
答案 2 :(得分:0)
虽然已经很晚了,但可以使用Android percentageRelativeLayout来完成 https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html 可以使用具有某些属性的百分比,但不支持所有属性。像填充仍然不支持百分比,而边距是。
答案 3 :(得分:0)
可以在ConstraintLayout中引入Guidelines。
以下是TextView放置在屏幕宽度的60%和屏幕高度的25%的示例:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="@+id/guideline1"
app:layout_constraintTop_toTopOf="@+id/guideline2" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline1"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline2"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />
</android.support.constraint.ConstraintLayout>