0

這是LinearLayout中的XML包含兩種觀點:中心包含其他視圖中的LinearLayout內查看

<RelativeLayout 
     android:id="@+id/addItems" 
     style="@android:style/Widget.ImageButton" 
     android:layout_weight="5" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:background="@drawable/add_product_selector" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal" 
     android:padding="10dp" > 

     <ImageView 
      android:id="@+id/addIcon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:background="@drawable/mbs_add_blue" 
      android:layout_marginLeft="45dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_toRightOf="@id/addIcon" 
      android:paddingLeft="5dp" 
      android:text="@string/add_product" 
      android:textSize="18dp" 
      android:textColor="@color/black" 
      android:textStyle="bold" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/startDictation" 
     style="@android:style/Widget.ImageButton" 
     android:layout_weight="1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:background="@drawable/add_product_selector" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal" 
     android:padding="10dp"> 

     <ImageView 
      android:id="@+id/recordIcon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:src="@drawable/microphone" 
      android:scaleType="fitCenter"/> 
    </RelativeLayout> 

</LinearLayout> 

而且它產生這樣的輸出:

enter image description here

因爲我需要以編程方式設置第二個RelativeLayout(id = startDictation)在某些情況下「消失」,我希望第一個RelativeLayout(id = addItems)及其子節點也以這些情況爲中心。因爲我在第一個ImageView(id = addIcon)上定義了layout_marginLeft =「45dp」,當第二個RelativeLayout(id = startDictation)消失時,我應該改變它以保持ImageView居中。

我必須要改變的東西在佈局,因爲如果我從第一個ImageView的去除layout_marginLeft =「45dp」(ID = addIcon)現在產生這樣的輸出:

enter image description here

回答

0

爲什麼不使用setVisibility(View.INVISIBLE)代替View.GONE

+0

因爲那麼第二個RelativeLayout的(id = startDictation)背景會變成白色 – user2468425

0

你試過用android:layout_gravity =「center_vertical」嗎?你可以選擇不同的值。

+0

請更具體地 – user2468425

+0

@ user2468425您是否可以包含您設置的線性佈局屬性以及它在上面的代碼中的缺失。 – Thilek

0

嘗試以編程方式設置佈局引力。當你打電話startDictation公開程度來走了使用此代碼

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); 
params.weight = 1.0f; 
params.gravity = Gravity.CENTER_VERTICAL; 

addItems.setLayoutParams(params); 

也不要忘了reaarange PARAMS當你看到你的startdictation佈局

+0

根本不起作用 – user2468425

+0

需要檢查你的java代碼 – Ajinkya

+0

問題是ImageView沒有水平居中,設置那些參數不能工作 – user2468425

1

問題解決了設置可見走在了ImageView的,而不是在RelativeLayout的

相關問題