2017-09-24 44 views
0

我有一個Imagebutton,一個水平線,它應該在圖像按鈕之下,並且在這條水平線下方,我有一個Textview。LinearLayout中的中心圖像按鈕

不知怎的,我不能居中的ImageButton,儘管使用android:gravity="center"

而且我也不能設置的ImageButton和的TextView之間的水平線。請幫助

about.xml

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <ImageButton 
     android:id="@+id/imageButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="32dp" 
     android:background="@drawable/roundcorner" 
     android:gravity="center" 
     app:srcCompat="@drawable/ic_launcher" /> 


    <View 
     android:layout_width="fill_parent" 
     android:layout_height="2dp" 
     android:background="#c0c0c0" 
     android:foregroundGravity="center_horizontal" 
     android:layout_alignParentTop="true" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="1dp" /> 

</LinearLayout> 

回答

3

與LinearLayout中工作,兩個屬性是視圖定位 - >

1)安卓重力
2)安卓layout_gravity

他們的名字應該可以幫助您:

android:gravity設置內容的嚴重性查看它的u sed on。

android:layout_gravity設置視圖或佈局在其父 的嚴重性。

1

使用android:layout_gravity="center",而不是android:gravity="center"ImageButton

+0

謝謝你,作品像魅力。兩者的主要區別是什麼? – Blnpwr

+1

「gravity」指定其子的重力,「layout_gravity」指定其在父級中的重力。 –

+1

我看到謝謝你。我可以在7分鐘內接受答案,別擔心,不會忘記它。 – Blnpwr