這是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>
而且它產生這樣的輸出:
因爲我需要以編程方式設置第二個RelativeLayout(id = startDictation)在某些情況下「消失」,我希望第一個RelativeLayout(id = addItems)及其子節點也以這些情況爲中心。因爲我在第一個ImageView(id = addIcon)上定義了layout_marginLeft =「45dp」,當第二個RelativeLayout(id = startDictation)消失時,我應該改變它以保持ImageView居中。
我必須要改變的東西在佈局,因爲如果我從第一個ImageView的去除layout_marginLeft =「45dp」(ID = addIcon)現在產生這樣的輸出:
因爲那麼第二個RelativeLayout的(id = startDictation)背景會變成白色 – user2468425