Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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 如何在元素之间划分空间?安卓_Android_Xml_Android Layout - Fatal编程技术网

Android 如何在元素之间划分空间?安卓

Android 如何在元素之间划分空间?安卓,android,xml,android-layout,Android,Xml,Android Layout,最后我遇到了一些有趣的问题。当我想在屏幕上的一些元素之间精确地划分位置时,我通常使用线性布局,将元素放入其中并给它们一些权重。它适用于2个元素和更多的元素,但当我想在更多不同的部分(20 | 60 | 20)上划分屏幕,然后也划分它们时,它会变得非常令人沮丧 我该怎么做?我希望我的布局在不同的屏幕上看起来是一样的,所以边距在小屏幕和大屏幕上看起来是不同的。例如,如果我想让我的按钮占据屏幕宽度的60%,我必须在两侧增加20%的空间 实际上,我是使用权重实现的,但我的xml代码如下所示: <L

最后我遇到了一些有趣的问题。当我想在屏幕上的一些元素之间精确地划分位置时,我通常使用线性布局,将元素放入其中并给它们一些权重。它适用于2个元素和更多的元素,但当我想在更多不同的部分(20 | 60 | 20)上划分屏幕,然后也划分它们时,它会变得非常令人沮丧

我该怎么做?我希望我的布局在不同的屏幕上看起来是一样的,所以边距在小屏幕和大屏幕上看起来是不同的。例如,如果我想让我的按钮占据屏幕宽度的60%,我必须在两侧增加20%的空间

实际上,我是使用权重实现的,但我的xml代码如下所示:

<LinearLayout
. . .
android:orientation="vertical"
android:weightSum="0">


<TextView
    . . .
    android:layout_weight="67"/>

<LinearLayout
    . . .
    android:orientation="horizontal"
    android:layout_weight="59"
    android:weightSum="0">


    <TextView
        . . .
        android:layout_weight="80"/>

    <ImageView
        . . .
        android:layout_weight="10"/>

    <TextView
        . . .
        android:layout_weight="80"/>



</LinearLayout>

<TextView
    . . .
    android:layout_weight="50"/>

<LinearLayout
    . . .
    android:orientation="horizontal"        
    android:weightSum="0">


    <TextView
        . . .
        android:layout_weight="70"/>

    <LinearLayout
        . . .
        android:orientation="vertical"
        android:weightSum="0">

        <TextView
            . . .
            android:layout_weight="65"/>

        <EditText
            . . .
            android:layout_weight="50"/>

        <TextView
            . . .
            android:layout_weight="75"/>

        <EditText
            . . .
            android:layout_weight="50"/>

        <TextView
            . . .
            android:layout_weight="65"/>


        </LinearLayout>

    <TextView
        . . .
        android:layout_weight="70"/>


</LinearLayout>

<LinearLayout
    . . .
    android:orientation="horizontal"
    android:weightSum="0">

    <TextView
        . . .
        android:layout_weight="80"/>

    <LinearLayout
        . . .
        android:orientation="vertical"
        android:layout_weight="60">

        <TextView
            . . .
            android:layout_weight="80"/>

        <Button
            . . .              
            android:layout_weight="60"/>

        <TextView
            . . .
            android:layout_weight="50"/>

    </LinearLayout>


    <TextView
        . . .
        android:layout_weight="80"/>



</LinearLayout>

<LinearLayout
    . . .
    android:orientation="horizontal"
    android:layout_weight="65"
    android:weightSum="0"
    >

    <Button            
        android:layout_weight="1"
        . . ./>

    <Button            
        android:layout_weight="1"
        . . ./>

    <Button            
        android:layout_weight="1"
        . . ./>

</LinearLayout>

正如您所看到的,有很多空的文本视图,除了在布局中占据某些位置之外,它们什么也不做。有没有更聪明、更不令人沮丧的方法来精确地划分屏幕上各个元素的位置


很高兴听到任何建议

当使用线性布局和权重时,有两件事是不明显的:

  • 将LinearLayout的weightSum设置为要划分的权重总和
  • 如果对宽度使用权重,则LinearLayout中的所有项目的布局宽度都应设置为0(是的,0…这也很重要)
  • 权重和权重和不必是整数;例如,可以将权重指定为2.5
  • 所以,为了直接回答你的问题,这里是我认为你想要的布局。我做了一个简单的水平线布局,左右两侧各有一个空白视图,权重为20%,然后中间有一个按钮,权重始终为60%。以下是它在各种屏幕大小上的外观,下面是XML代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:orientation="horizontal"
      android:background="@android:color/holo_blue_bright"
      android:weightSum="100.0"
      >
      <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:background="@android:color/holo_green_dark"
        />
      <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="60.0"
        android:background="@android:color/holo_red_light"
        android:text="Hello, I am button"
        />
      <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:background="@android:color/holo_green_dark"
        />
    </LinearLayout>
    

    
    
    编辑:这是更新后的XML,它具有嵌套的权重,对性能很糟糕(请参见注释)。。。建议使用相对长度和居中,以dp测量边距/填充,以保持屏幕尺寸之间的一致性

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:orientation="horizontal"
      android:background="@android:color/holo_blue_bright"
      android:weightSum="100.0"
      >
      <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:background="@android:color/holo_green_dark"
        />
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="60.0"
        android:weightSum="99.9"
        >
        <Button
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="33.3"
          android:text="Hello, I am button1"
          android:background="@android:color/holo_red_light"
          />
        <Button
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="33.3"
          android:text="Hello, I am button2"
          android:background="@android:color/holo_red_light"
          />
        <Button
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="33.3"
          android:text="Hello, I am button3"
          android:background="@android:color/holo_red_light"
          />
      </LinearLayout>
      <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:background="@android:color/holo_green_dark"
        />
    </LinearLayout>
    

    当使用线性布局和权重时,有几件事需要记住,但并不明显:

  • 将LinearLayout的weightSum设置为要划分的权重总和
  • 如果对宽度使用权重,则LinearLayout中的所有项目的布局宽度都应设置为0(是的,0…这也很重要)
  • 权重和权重和不必是整数;例如,可以将权重指定为2.5
  • 所以,为了直接回答你的问题,这里是我认为你想要的布局。我做了一个简单的水平线布局,左右两侧各有一个空白视图,权重为20%,然后中间有一个按钮,权重始终为60%。以下是它在各种屏幕大小上的外观,下面是XML代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:orientation="horizontal"
      android:background="@android:color/holo_blue_bright"
      android:weightSum="100.0"
      >
      <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:background="@android:color/holo_green_dark"
        />
      <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="60.0"
        android:background="@android:color/holo_red_light"
        android:text="Hello, I am button"
        />
      <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:background="@android:color/holo_green_dark"
        />
    </LinearLayout>
    

    
    
    编辑:这是更新后的XML,它具有嵌套的权重,对性能很糟糕(请参见注释)。。。建议使用相对长度和居中,以dp测量边距/填充,以保持屏幕尺寸之间的一致性

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="100dp"
      android:orientation="horizontal"
      android:background="@android:color/holo_blue_bright"
      android:weightSum="100.0"
      >
      <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:background="@android:color/holo_green_dark"
        />
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="60.0"
        android:weightSum="99.9"
        >
        <Button
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="33.3"
          android:text="Hello, I am button1"
          android:background="@android:color/holo_red_light"
          />
        <Button
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="33.3"
          android:text="Hello, I am button2"
          android:background="@android:color/holo_red_light"
          />
        <Button
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="33.3"
          android:text="Hello, I am button3"
          android:background="@android:color/holo_red_light"
          />
      </LinearLayout>
      <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20.0"
        android:background="@android:color/holo_green_dark"
        />
    </LinearLayout>
    

    因为您需要嵌套权重来完成您想做的事情,所以在Java代码中,在ViewTreeObserver.OnGlobalLayoutListener中,这样做效率要高得多。下面是一个如何执行此操作的示例(此外,它在初始布局期间只触发一次,然后将其自身移除,这样您就不会无故重复执行此操作):

    当然,您可以用另一个RelativeLayout或LinearLayout替换该按钮,以使视图看起来符合您的需要,但在onGloablLayout()方法中,您可以确保屏幕测量值有效。在这里,您可以使用Java在一个布局周期内手动调整所有嵌套百分比,而不是使用嵌套权重时所受到的指数性能影响

    如果您需要进一步澄清,请告诉我

    此示例位于片段的onCreateView()覆盖中,通常可以在其中查找视图并设置它们的样式:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        final View createdView = super.onCreateView(inflater, container, savedInstanceState);
        final Button button = (Button) createdView.findViewById(R.id.i_am_button);
        createdView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout()
            {
                DisplayMetrics displayMetrics = createdView.getContext().getResources().getDisplayMetrics();
                Log.d(TAG, "Screen Width x Height in Pixels: " + displayMetrics.widthPixels + " x " + displayMetrics.heightPixels);
                button.getLayoutParams().width = 0.60 * displayMetrics.widthPixels;
                button.requestLayout();
                createdView.getViewTreeObserver().removeOnGlobalLayoutListener(this);  //One-shot!
            }
        });
        return createdView;
    }
    
    这将假定一个XML布局,其中一个按钮水平居中于RelativeLayout中,以便按照您所描述的方式工作

    为了清晰起见,下面是这样一个示例XML片段:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="150dp"
      android:background="@android:color/holo_blue_bright"
      >
      <Button
        android:id="@+id/i_am_button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_red_dark"
        android:text="Hello, I am button"
        />
    </RelativeLayout>
    

    因为您需要嵌套权重来完成您想做的事情,所以在Java代码中,在ViewTreeObserver.OnGlobalLayoutListener中,这样做效率要高得多。下面是一个如何执行此操作的示例(此外,它在初始布局期间只触发一次,然后将其自身移除,这样您就不会无故重复执行此操作):

    当然,您可以用另一个RelativeLayout或LinearLayout替换该按钮,以使视图看起来符合您的需要,但在onGloablLayout()方法中,您可以确保屏幕测量值有效。在这里,您可以使用Java在一个布局周期内手动调整所有嵌套百分比,而不是使用嵌套权重时所受到的指数性能影响

    如果您需要进一步澄清,请告诉我

    此示例位于片段的onCreateView()覆盖中,通常可以在其中查找视图并设置它们的样式:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        final View createdView = super.onCreateView(inflater, container, savedInstanceState);
        final Button button = (Button) createdView.findViewById(R.id.i_am_button);
        createdView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout()
            {
                DisplayMetrics displayMetrics = createdView.getContext().getResources().getDisplayMetrics();
                Log.d(TAG, "Screen Width x Height in Pixels: " + displayMetrics.widthPixels + " x " + displayMetrics.heightPixels);
                button.getLayoutParams().width = 0.60 * displayMetrics.widthPixels;
                button.requestLayout();
                createdView.getViewTreeObserver().removeOnGlobalLayoutListener(this);  //One-shot!
            }
        });
        return createdView;
    }
    
    这将假定一个XML布局,其中一个按钮水平居中于RelativeLayout中,以便按照您所描述的方式工作

    为了清晰起见,下面是这样一个示例XML片段:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="150dp"
      android:background="@android:color/holo_blue_bright"
      >
      <Button
        android:id="@+id/i_am_button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:background="@android:color/holo_red_dark"
        android:text="Hello, I am button"
        />
    </RelativeLayout>
    
    
    
    非常感谢您提供了大量有用的信息!请你再澄清一点好吗。如果我们想从您的示例中垂直分割中间的红色空间,该怎么办?有没有一种方法可以避免嵌套权重;这正是助教