【问题标题】:Android Relative Layout Align CenterAndroid 相对布局对齐中心
【发布时间】:2012-12-10 08:32:15
【问题描述】:

我正在尝试按如下方式创建列表活动项布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView 
        android:contentDescription="ss"
        android:id="@+id/place_category_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="15dp"
        android:paddingTop="10dp" android:src="@drawable/marker"/>

    <TextView
        android:id="@+id/place_distance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="320" />

    <TextView
        android:id="@+id/place_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/place_category_icon"
        android:text="Place Name"
        android:textColor="#FFFF00"
        android:textSize="14sp"
        android:textStyle="bold" />
</RelativeLayout>

我希望布局显示如下。

我想把它水平居中对齐

【问题讨论】:

    标签: android android-relativelayout


    【解决方案1】:

    我希望这会奏效

    已编辑

      <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingRight="15dp" >
    
        <ImageView
            android:id="@+id/place_category_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:contentDescription="ss"
            android:paddingTop="10dp"
            android:src="@drawable/ic_launcher" />
    
        <TextView
            android:id="@+id/place_distance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="320" />
    
        <TextView
            android:id="@+id/place_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/place_category_icon"
            android:text="Place Name"
            android:textColor="#FFFF00"
            android:textSize="14sp"
            android:textStyle="bold" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      如果您想让它居中,请在 TextView 中使用 android:layout_centerVertical="true"

      【讨论】:

      • 我的相对布局的高度和宽度都有 fill_parent。由于这是 ListView 内的 Item 的布局,我希望它们中的许多堆叠在彼此之上。所以我需要对 RelativeView 做 wrap_contnet 吗?
      • @HarshaMV 实际上没关系,因为它只适用于单个项目。
      【解决方案3】:

      这绝对适合你。

      <RelativeLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/top_bg" >
      
          <Button
              android:id="@+id/btn_report_lbAlert"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_centerVertical="true"
              android:layout_marginLeft="@dimen/btn_back_margin_left"
              android:background="@drawable/btn_edit" />
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:layout_centerVertical="true"
              android:text="FlitsLimburg"
              android:textColor="@color/white"
              android:textSize="@dimen/tv_header_text"
              android:textStyle="bold" />
      
          <Button
              android:id="@+id/btn_refresh_lbAlert"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:layout_centerVertical="true"
              android:layout_marginRight="@dimen/btn_back_margin_right"
              android:background="@drawable/btn_refresh" />
      </RelativeLayout>
      

      【讨论】:

        【解决方案4】:

        您可以使用重力来对齐顶部和底部。

         android:gravity="center_vertical"
         android:layout_alignTop="@id/place_category_icon"
         android:layout_alignBottom="@id/place_category_icon"
        

        【讨论】:

          【解决方案5】:

          在你的 RelativeLayout 中使用它

          android:gravity="center_vertical"

          【讨论】:

            【解决方案6】:

            这是你需要的吗?

            <?xml version="1.0" encoding="utf-8"?>
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" >
            
                    <ImageView
                        android:id="@+id/place_category_icon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:contentDescription="ss"
                        android:paddingRight="15dp"
                        android:paddingTop="10dp"
                        android:src="@drawable/marker" />
            
                    <TextView
                        android:id="@+id/place_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Place Name"
                        android:textColor="#F00F00"
                        android:layout_gravity="center_vertical"
                        android:textSize="14sp"
                        android:textStyle="bold" />
            
                    <TextView
                        android:id="@+id/place_distance"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="0"
                        android:layout_gravity="center_vertical"
                        android:text="320" />
                </TableRow>
            
            </RelativeLayout>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多