简体   繁体   English

Android在充气布局时动态居中按钮

[英]Android Dynamically center the buttons when inflating layout

I need to center layout dynamically, but the problem is I use inflate method to add the layout. 我需要动态居中布局,但问题是我使用inflate方法添加布局。 And I want buttons to be center. 我希望按钮成为中心。

I tried adding xml attributes like gravity, layout_gravity. 我尝试添加像gravity,layout_gravity这样的xml属性。 But it didn't work. 但它没有用。 And I tried to add parameters, but since I inflate xml I can't add parameters. 我试图添加参数,但由于我膨胀xml我无法添加参数。 Because I can't use addRule method. 因为我不能使用addRule方法。

Then I tried to make every element center by adding gravity attribute to root layout, it didn't work out either. 然后我尝试通过将重力属性添加到根布局来使每个元素居中,它也没有成功。

Here is the my code. 这是我的代码。

actvity_main.xml actvity_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>

three_buttons_layout.xml three_buttons_layout.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>

And here is my java code 这是我的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);
            }
        });

I want Button1, Button2, and Button3 to be center. 我希望Button1,Button2和Button3成为中心。

截图

A horizontal LinearLayout will always left-align its subviews. 水平LinearLayout将始终左对齐其子视图。 You're going to have to use a RelativeLayout in order to get the positioning you want. 你将不得不使用RelativeLayout来获得你想要的定位。 I'd suggest giving button1 the android:layout_alignParentLeft attribute, button2 the android:layout_centerHorizontal attribute and button3 the android:layout_alignParentRight attribute, like this: 我建议给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>

(I'm just editing your code in the comment editor, so there might be a syntax error in there somewhere, but you get the idea.) (我只是在评论编辑器中编辑你的代码,因此某处可能存在语法错误,但你明白了。)

Also, you should be able to set any attribute you want on those buttons after inflating the view. 此外,您应该能够在充气视图后在这些按钮上设置所需的任何属性。 Just call findViewById(R.id.button1) and it should return you the view you want (as long as its after the inflater.inflate call). 只需调用findViewById(R.id.button1),它就会返回你想要的视图(只要在inflater.inflate调用之后)。 On that note, you've given all your buttons the same id, which probably isn't a good idea. 在那个注释中,你已经给了你所有的按钮相同的ID,这可能不是一个好主意。

Hope that helps! 希望有所帮助! Let me know if it's still not working. 如果它仍然不起作用,请告诉我。

Edit: I just realized you're going to need that RelativeLayout to have android:layout_width="match_parent". 编辑:我刚刚意识到你需要那个RelativeLayout来安装android:layout_width =“match_parent”。 I've edited the above xml accordingly. 我已经相应地编辑了上面的xml。

//try this //尝试这个

<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>

//your can remove this below line from parent to make it as center horizontally //您可以从父级中删除此行以使其成为水平居中

android:layout_gravity="center"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM