Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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_Android Layout - Fatal编程技术网

Android 捕捉元素/条件布局

Android 捕捉元素/条件布局,android,android-layout,Android,Android Layout,很抱歉这个非常糟糕的标题,我不知道该怎么称呼这个问题。我想做的是:拥有一个有两个孩子的亲戚: 布局为\u centerInParent=true的一个 布局为\u alignParentBottom=true的一个 但是,当设备处于横向模式时,元件1略高于或低于元件2。但是,元素1上方有足够的空间显示在元素2上方。如果屏幕太小,无法将元素1居中,并且两个元素不重叠,那么如何进行布局,然后将元素1与元素2上方的布局一样对齐?好的,如果元素1的资源不太密集,这里有一个左字段建议可能会起作用 元素1有

很抱歉这个非常糟糕的标题,我不知道该怎么称呼这个问题。我想做的是:拥有一个有两个孩子的亲戚:

布局为\u centerInParent=true的一个 布局为\u alignParentBottom=true的一个
但是,当设备处于横向模式时,元件1略高于或低于元件2。但是,元素1上方有足够的空间显示在元素2上方。如果屏幕太小,无法将元素1居中,并且两个元素不重叠,那么如何进行布局,然后将元素1与元素2上方的布局一样对齐?

好的,如果元素1的资源不太密集,这里有一个左字段建议可能会起作用


元素1有两个版本,第一个版本的layout_centerInParent=true,第二个版本的layout_over=@id\element2。将它们都默认为安卓:可见性=不可见。在onCreate帖子中,一个Runnable检查两个元素的Y维度,并设置View.VISIBLE在元素上的最小值。

好的,如果元素1不是资源密集型的,这里有一个leftfield建议可能有效

元素1有两个版本,第一个版本的layout_centerInParent=true,第二个版本的layout_over=@id\element2。将它们都默认为安卓:可见性=不可见。在onCreate Post中,一个Runnable检查两个元素的Y维度,并将View.VISIBLE设置为元素上的最小值。

新想法:

这是另一个解决方案,它不是很优雅,但很简单。将元素1放入其自己的中,配置如下:

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" // More on this line below
    android:layout_alignParentTop="true"
    android:layout_above="@id/element2"
    android:gravity="center">
<> P>线性布局总是从屏幕的顶部一直延伸到元素2之上,并且因为它的重心是中心,元素1总是在任何可用的空间的中间。注意:行android:layout\u height=。。。是必需的,但实际上似乎没有做任何事情。无论是“填充”父内容还是“包裹”内容,其高度都由将其顶部固定到“父”顶部、底部固定到“图元2”的线覆盖。但是,没有该行会导致运行时异常崩溃

原创,不是很好的主意:

另一个不太理想的解决方案是使用两个布局文件。将纵向布局xml放入布局文件夹,将横向布局放入名为layout land的新文件夹。Android会根据手机的方位自动选择合适的手机。我认为您可以使用布局端口,但将纵向端口放入just layout可确保它知道默认情况下使用哪个端口。

新想法:

这是另一个解决方案,它不是很优雅,但很简单。将元素1放入其自己的中,配置如下:

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" // More on this line below
    android:layout_alignParentTop="true"
    android:layout_above="@id/element2"
    android:gravity="center">
<> P>线性布局总是从屏幕的顶部一直延伸到元素2之上,并且因为它的重心是中心,元素1总是在任何可用的空间的中间。注意:行android:layout\u height=。。。是必需的,但实际上似乎没有做任何事情。无论是“填充”父内容还是“包裹”内容,其高度都由将其顶部固定到“父”顶部、底部固定到“图元2”的线覆盖。但是,没有该行会导致运行时异常崩溃

原创,不是很好的主意:


另一个不太理想的解决方案是使用两个布局文件。将纵向布局xml放入布局文件夹,将横向布局放入名为layout land的新文件夹。Android会根据手机的方位自动选择合适的手机。我认为您可以使用布局端口,但将纵向端口放在just layout中可以确保它知道默认情况下使用哪个端口。

这应该可以为您提供一个布局,其中text1项填充text2上方的可用空间,并在该空间中垂直和水平居中;文本2位于底部,水平居中

<LinearLayout 
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <TextView
        android:id="@android:id/text1" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:text="Centered" 
        android:gravity="center_vertical|center_horizontal"
        />

    <TextView
        android:id="@android:id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Bottom"
        android:gravity="center_horizontal"
        />
</LinearLayout>

重要的部分是android:layout\u weight,再加上android:gravity。

这应该可以为您提供一个布局,其中text1项填充text2上方的可用空间,并在该空间中垂直和水平居中;文本2位于底部,水平居中

<LinearLayout 
    android:orientation="vertical"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">

    <TextView
        android:id="@android:id/text1" 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:layout_weight="1" 
        android:text="Centered" 
        android:gravity="center_vertical|center_horizontal"
        />

    <TextView
        android:id="@android:id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Bottom"
        android:gravity="center_horizontal"
        />
</LinearLayout>

重要的部分是android:layout\u weight,再加上android:gravity。

对于这个解决方案,我不得不使用.postDelayed.post不起作用,这对我来说非常难看。不过,对于这个想法,+1。对于这个解决方案,我不得不使用.postDelayed.post不起作用,这对我来说非常难看。尽管如此,这个想法还是+1。这主意不错,但即使设备处于横向模式,它也可能有足够的空间将元素1放在中间。如果它是一个非常大的设备,它实际上可能看起来很尴尬。感谢你的新想法,我会稍后再尝试一下。这是一个不错的想法,但即使设备处于横向模式,它也可能有足够的空间来容纳中间的元素1。如果它是一个非常大的设备,它可能看起来很尴尬。谢谢
对于新的想法,我会稍后再尝试,这就是我最终的想法。将RelativeLayout更改为LinearLayout,并使用您建议的重量/重力组合。谢谢。这就是我最后选择的。将RelativeLayout更改为LinearLayout,并使用您建议的重量/重力组合。谢谢