开发者

Android: Center ImageButton in LinearLayout

How can I center ImageButton in the this LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main"
>

ImageButton

              <ImageButton android:id="@+id/btnBut"
                      android:background="@drawable/button"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_c开发者_开发百科ontent" />

I don't want to use fixed sizes.

Thanks


Use android:layout_gravity attribute:

 <ImageButton android:id="@+id/btnBut"
    android:background="@drawable/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />


The following code works for me:

 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_alignParentBottom="true"
  android:gravity="center_horizontal"
 >
  <ImageButton
   android:id="@+id/btnBut"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/button"
   >
  </ImageButton>
 </LinearLayout>


If you want to center content in a linerlayout, you have to set android:orientation="vertical" . it is useless to talk center in android:orientation="horizontal". I think you understand the reason now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜