简体   繁体   English

中心布局Android

[英]Center layout Android

How can I center these buttons on Android? 如何在Android上居中这些按钮? 替代文字

The code I'm using in the layout is: 我在layout使用的代码是:

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

      <TextView android:layout_width="fill_parent" 
           android:layout_height="wrap_content" 
           android:text="@string/hello" />

      <LinearLayout android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:orientation="horizontal">

           <Button android:text="Test"
                android:layout_width="100px"
                android:layout_height="40px" />
           <Button android:text="Test"
                android:layout_width="100px"
                android:layout_height="40px" />
           <Button android:text="Test"
                android:layout_width="100px"
                android:layout_height="40px" />
           <Button android:text="Test"
                android:layout_width="100px"
                android:layout_height="40px" />
      </LinearLayout>
</LinearLayout>

Make the LinearLayout containing the buttons to wrap_content on width and to have center_horizontal on gravity. 使LinearLayout包含宽度上的wrap_content按钮,并在重力上包含center_horizo​​ntal。

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

 <TextView android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" />

 <LinearLayout android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:orientation="horizontal"
      android:layout_gravity="center_horizontal">
     <Button android:text="Test"
       android:layout_width="100px"
       android:layout_height="40px" />
     <Button android:text="Test"
       android:layout_width="100px"
       android:layout_height="40px" />
     <Button android:text="Test"
       android:layout_width="100px"
       android:layout_height="40px" />
     <Button android:text="Test"
       android:layout_width="100px"
       android:layout_height="40px" />
       </LinearLayout>
</LinearLayout>

I'd like to know if anyone else has any comments on this as well. 我想知道是否还有其他人对此有任何意见。 I've always done it with a TableLayout and set both stretchColumns and collapseColumns on all columns so it scales on all screen sizes. 我总是使用TableLayout完成它并在所有列上设置stretchColumns和collapseColumns,以便在所有屏幕尺寸上进行缩放。 A little like this: 有点像这样:

 <TableLayout
  android:layout_width="fill_parent"
  android:layout_height="47dp"
  android:stretchColumns="*"
  android:collapseColumns="*"
  android:padding="0px"
  android:background="@drawable/bottom_bg">
  <TableRow>
    <ImageView
        android:id="@+id/article_button_home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_home_selector"
        android:layout_centerHorizontal="true" />
   <ImageView
        android:id="@+id/article_button_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_sharearticle_selector"
        android:layout_centerHorizontal="true" />
    <ImageView
        android:id="@+id/article_button_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/button_settings_selector" 
        android:layout_centerHorizontal="true"/>
    <ImageView
        android:id="@+id/article_button_about"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true"      
        android:src="@drawable/button_about_selector"/>
</TableRow>
</TableLayout>

I do wonder if it is the best way though. 我不知道这是不是最好的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM