簡體   English   中英

Android在充氣布局時動態居中按鈕

[英]Android Dynamically center the buttons when inflating layout

我需要動態居中布局,但問題是我使用inflate方法添加布局。 我希望按鈕成為中心。

我嘗試添加像gravity,layout_gravity這樣的xml屬性。 但它沒有用。 我試圖添加參數,但由於我膨脹xml我無法添加參數。 因為我不能使用addRule方法。

然后我嘗試通過將重力屬性添加到根布局來使每個元素居中,它也沒有成功。

這是我的代碼。

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

<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成為中心。

截圖

水平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。

//嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM