【问题标题】:Relative Layout Gravity not Working相对布局重力不起作用
【发布时间】:2014-01-03 17:01:21
【问题描述】:

这是XML文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mylayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="bottom" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/whitebar" />

</RelativeLayout>

这是 Java 文件

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.mylayout);
    int images[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4};

    relativeLayout.setBackgroundResource(images[getRandomNumber()]);
}


private int getRandomNumber() {
    //Note that general syntax is Random().nextInt(n)
    //It results in range 0-4
    //So it should be equal to number of images in images[] array
    return new Random().nextInt(4);
}}

The code above gives an output

BUT我想发生的事情类似于

由于相对布局重力不起作用,如何设置我的白条图像的确切位置?

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    布局重力用于线性布局。

    你要使用的是alignParentBottom

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mylayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/whitebar" />
    
    </RelativeLayout>
    

    【讨论】:

    • 您好,谢谢您的回答,我仍然有问题。请看这个。 image 我认为问题出在那个蓝色矩形上?如何解决?
    • 将 android:scaleType="fitXY" 添加到图像视图中
    【解决方案2】:

    你应该移除重力并放入 ImageView

    android:layout_alignParentBottom="true"
    

    【讨论】:

    • 您好,谢谢您的回答,我仍然有问题。请看这个。 image 我认为问题出在那个蓝色矩形上?如何解决?
    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    相关资源
    最近更新 更多