Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android XML中的边距/填充百分比_Android_Layout - Fatal编程技术网

Android XML中的边距/填充百分比

Android XML中的边距/填充百分比,android,layout,Android,Layout,我是android开发的新手。我想用Xml设置边距和填充,不是用dp或px,而是用百分比。有什么方法可以做到这一点吗?这是不可能的,尽管您最好在java代码中选择屏幕的宽度和高度,然后 Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); 然后根据屏幕大小计算您的边距,并将其设置为 setm

我是android开发的新手。我想用Xml设置边距和填充,不是用dp或px,而是用百分比。有什么方法可以做到这一点吗?

这是不可能的,尽管您最好在java代码中选择屏幕的宽度和高度,然后

 Display display = getWindowManager().getDefaultDisplay(); 
 int width = display.getWidth();
 int height = display.getHeight();
然后根据屏幕大小计算您的边距,并将其设置为

  setmargin(int x) //method of view/layout.

通过这种方式,您可以根据屏幕大小设置边距,而不是为所有屏幕设置固定的边距,这基本上类似于以百分比为单位进行设置,但来自Java代码。

如果您试图以百分比为单位在控件之间共享sreen宽度。在android中实现这一点的最佳方法是为它们使用
weight
属性。查看以下URL以了解更多详细信息

问候,,
Aqif Hamid

虽然已经很晚了,但可以用Android percentageRelativeLayout完成 可以对某些属性使用百分比,但不支持所有属性。Like padding在百分比上仍然不受支持,而margin是。

在ConstraintLayout中引入Like padding成为可能

下面是TextView的示例,它位于60%的屏幕宽度和25%的屏幕高度:



嘿,我认为使用getWidth()是不可能的,并且getHeight()在API级别13+中被弃用。改为使用getSize(point)。@maraci检查我的另一个答案以了解您的问题链接>>
<?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>