2

我需要动态居中布局,但问题是我使用 inflate 方法添加布局。我希望按钮成为中心。

我尝试添加 xml 属性,如重力、layout_gravity。但它没有用。我尝试添加参数,但由于我膨胀了 xml,我无法添加参数。因为我不能使用 addRule 方法。

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

这是我的代码。

活动主文件.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>

三按钮布局.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    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_marginBottom="29dp"
        android:text="Button 1" />

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

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

</LinearLayout>

这是我的java代码

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     parent = (ViewGroup)findViewById(R.id.buttons_layout);

     captureButton.setOnClickListener(new OnClickListener() {

     public void onClick(View v) {
        camera.takePicture(null, null, photoCallback);
        captureButton.setVisibility(View.GONE);
        inflater.inflate(R.layout.three_buttons_layout, parent, true);
            }
        });

我希望 Button1、Button2 和 Button3 居中。

截屏

4

2 回答 2

1

水平 LinearLayout 将始终左对齐其子视图。您将不得不使用 RelativeLayout 以获得您想要的定位。我建议给 button1 android:layout_alignParentLeft 属性,button2 android:layout_centerHorizo​​ntal 属性和 button3 android:layout_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) 它就会返回您想要的视图(只要它在 inflater.inflate 调用之后)。在那张纸条上,您已经为所有按钮提供了相同的 id,这可能不是一个好主意。

希望有帮助!让我知道它是否仍然无法正常工作。

编辑:我刚刚意识到你需要那个RelativeLayout才能拥有android:layout_width =“match_parent”。我已经相应地编辑了上面的 xml。

于 2012-09-20T12:56:28.673 回答
1

//试试这个

<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"
于 2012-09-20T13:01:01.183 回答