Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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 Relativelayout_Vertical Alignment - Fatal编程技术网

Android 相对线的垂直对齐组件

Android 相对线的垂直对齐组件,android,android-relativelayout,vertical-alignment,Android,Android Relativelayout,Vertical Alignment,在我的相对布局中,我有两个视图,一个是TextView,一个是ImageView。以下是我想要的结果: 1) 如果imageview处于隐藏状态(如果满足某些条件,可以通过编程将其设置为visibility=gone),则textview将在相对布局中垂直和水平居中对齐 2) 如果imageview未隐藏,则textview应水平居中对齐,但垂直方向上不会占用超过需要的空间,imageview会占用所有可用高度 这是我正在使用的代码。但它不起作用,因为如果我将imageview设置为“填充父对象

在我的相对布局中,我有两个视图,一个是TextView,一个是ImageView。以下是我想要的结果:

1) 如果imageview处于隐藏状态(如果满足某些条件,可以通过编程将其设置为visibility=gone),则textview将在相对布局中垂直和水平居中对齐

2) 如果imageview未隐藏,则textview应水平居中对齐,但垂直方向上不会占用超过需要的空间,imageview会占用所有可用高度

这是我正在使用的代码。但它不起作用,因为如果我将imageview设置为“填充父对象”,它会与文本重叠,如果我将其设置为“包裹内容”,它会太小:

<RelativeLayout
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_centerVertical="true"
   android:layout_above="@+id/linearlayout_buttons">

<TextView
       android:id="@+id/text_compliance_question"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:textSize="22sp"
       android:layout_alignWithParentIfMissing="true"
       android:layout_above="@+id/image_thumbnail"
       android:gravity="center"
       android:textStyle="bold" />

<ImageView
       android:visibility="visible"
       android:id="@+id/image_thumbnail"
       android:layout_marginTop="30dp"
       android:layout_marginBottom="30dp"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       android:contentDescription="@string/title_activity" />   

</RelativeLayout>  


非常感谢您的任何想法。

使用以下布局文件可能会解决您的问题

<?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="match_parent"
    android:orientation="vertical"
    android:gravity="center_vertical"
    android:weightSum="1" >

    <TextView
        android:id="@+id/text_compliance_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:layout_gravity="center_horizontal"
        android:textSize="22sp"
        android:textStyle="bold"
        android:text="Hello world!" />

    <ImageView
        android:id="@+id/image_thumbnail"
        android:layout_weight="1"
        android:visibility="visible"
        android:layout_gravity="center_horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/device_type_windows" />

</LinearLayout>

当图像视图可见/消失时,它看起来像:

这是您的布局,希望这是您要找的

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_margin="20dp"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <LinearLayout
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="0dp">
            <TextView
                    android:gravity="center"
                    android:text="hello there"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            <ImageView
                    android:visibility="gone"
                    android:layout_weight="1"
                    android:src="@drawable/ic_launcher"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button
                android:text="dynamic button 1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        <Button
                android:text="dynamic button 2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        <Button
                android:text="dynamic button 3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
            android:layout_marginTop="20dp"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <Button
                android:text="dynamic button 2"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <Button
                android:layout_weight="1"
                android:text="dynamic button 3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>


使用RelativeLayout是否有特殊原因?使用weightSum进行线性布局可能会满足您的需要。谢谢您的回复。使用RelativeLayout没有特别的原因。如果我使用重量,它不会成为一个百分比游戏吗?我不需要按百分比来做,我只是希望事情尽可能多地占据空间。使用权重并不一定意味着它是按百分比来做的。将权重总和设置为“1”,并将ImageView的权重设置为“1”,ImageView将尽可能占用空间。您好,非常感谢。这接近我想要的,但是我如何得到它,使实际图像占据更多的空间?例如,Windows徽标可能会增长到显示大小的4倍左右?谢谢对不起,我不太明白你的问题。你是说你的图像比windows徽标大4倍还是比屏幕大?如答案所示,所有空间都可以用于图像,即蓝色矩形的高度和屏幕的宽度。查看新图片,只需更改imageView的src的布局宽度以填充父项。完美,更改为线性布局就完成了跟踪!非常感谢!