Java 当打开布局时,Android会动态将按钮居中

Java 当打开布局时,Android会动态将按钮居中,java,android,android-layout,layout-inflater,Java,Android,Android Layout,Layout Inflater,我需要动态居中布局,但问题是我使用充气方法添加布局。我想让按钮居中 我尝试添加xml属性,如重力、布局和重力。但它不起作用。 我试图添加参数,但由于我膨胀了xml,所以无法添加参数。因为我不能使用addRule方法 然后,我试图通过向根布局添加重力属性来使每个元素居中,但也没有成功 这是我的密码 Activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x

我需要动态居中布局,但问题是我使用充气方法添加布局。我想让按钮居中

我尝试添加xml属性,如重力、布局和重力。但它不起作用。 我试图添加参数,但由于我膨胀了xml,所以无法添加参数。因为我不能使用addRule方法

然后,我试图通过向根布局添加重力属性来使每个元素居中,但也没有成功

这是我的密码

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="match_parent"
        android:layout_height="335dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:id="@+id/buttons_layout"
        android:layout_below="@+id/surfaceView1"
        >

        <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="29dp"
        android:text="Button" />

    </RelativeLayout>



</RelativeLayout>
我希望按钮1、按钮2和按钮3居中


水平线性布局将始终左对齐其子视图。为了得到你想要的位置,你必须使用相对论。我建议为button1指定android:layout\u alignParentLeft属性,为button2指定android:layout\u Center水平属性,为button3指定android:layout\u alignParentRight属性,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="29dp"
    android:text="Button 1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="29dp"
    android:text="Button 2" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Button 3" />

</RelativeLayout>

(我只是在注释编辑器中编辑您的代码,因此其中可能有语法错误,但您明白了。)

此外,在膨胀视图后,您应该能够在这些按钮上设置任何想要的属性。只需调用findViewById(R.id.button1),它就会返回您想要的视图(只要在充气器.inflate调用之后)。在这一点上,您给所有按钮都提供了相同的id,这可能不是一个好主意

希望有帮助!如果仍然不起作用,请告诉我

编辑:我刚刚意识到你需要一个相对论来拥有android:layout\u width=“match\u parent”。我已经相应地编辑了上面的xml。

//试试这个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center" 
    >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" />

</LinearLayout>

给你的线性布局android:layout\u width=“fill\u parent”你想让你的按钮水平居中还是屏幕居中?我想让它们水平居中。@PadmaKumar我给了它android:layout\u width=“fill\u parent”,但它不起作用。和截图一样。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="29dp"
    android:text="Button 1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="29dp"
    android:text="Button 2" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:text="Button 3" />

</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center" 
    >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" />

</LinearLayout>
android:layout_gravity="center"