【问题标题】:how to align controls left, right and center in relative layout in android如何在android中的相对布局中将控件左、右和居中对齐
【发布时间】:2015-04-25 06:58:06
【问题描述】:

我有 3 个控件,我希望一个左对齐,另一个居中,最后一个在相对布局中一直向右。

我已经尝试过以下方法:

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

    <RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">


        <ImageView
            android:id="@+id/backimg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            android:background="@null"
            android:layout_alignParentLeft="true"

            />

        <ImageView
            android:id="@+id/logoimg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/logo"
            android:background="@null"
            android:layout_centerHorizontal="true"
            />

        <ImageButton
            android:id="@+id/closebtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/close"
            android:background="@null"
            android:layout_alignParentRight="true"
            />

    </RelativeLayout>



    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />

</LinearLayout>

这会将所有控件堆叠在一起。

我怎样才能让它看起来像这样:

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    将 imageViews 的宽度更改为 wrap_content,一切都会如你所愿 这是你的新代码

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="match_parent">
    
    <RelativeLayout android:id="@+id/RelativeLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
    
        <ImageView
            android:id="@+id/backimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back"
            android:background="@null"
            android:layout_alignParentLeft="true"
    
            />
    
        <ImageView
            android:id="@+id/logoimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo"
            android:background="@null"
            android:layout_centerHorizontal="true"
            />
    
        <ImageButton
            android:id="@+id/closebtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/close"
            android:background="@null"
            android:layout_alignParentRight="true"
            />
    
    </RelativeLayout>
    
    
    
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    

    【讨论】:

      【解决方案2】:

      您可以使用android:layout_toRightOf 告诉视图在另一个视图的右侧

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="match_parent">
      
          <RelativeLayout android:id="@+id/RelativeLayout01"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              xmlns:android="http://schemas.android.com/apk/res/android">
      
      
              <ImageView
                  android:id="@+id/backimg"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:src="@drawable/back"
                  android:background="@null"
                  android:layout_alignParentLeft="true"
      
                  />
      
              <ImageView
                  android:id="@+id/logoimg"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:src="@drawable/logo"
                  android:background="@null"
                  android:layout_toRightOf="@id/backimg"  
                  android:layout_centerHorizontal="true"
                  />
      
              <ImageButton
                  android:id="@+id/closebtn"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:src="@drawable/close"
                  android:background="@null"
                  android:layout_toRightOf="@id/logoimg"
                  android:layout_alignParentRight="true"
                  />
      
          </RelativeLayout>
      
      
      
          <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/webview"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              />
      
      </LinearLayout>
      

      【讨论】:

      • android:id="@+id/logoimg" android:layout_toRightOf="@id/logoing" id 不一样
      • 不,我没试过
      • 所以你只有一个打字错误
      • 是的。感谢您的关注.. 已修复
      猜你喜欢
      • 2018-12-15
      • 1970-01-01
      • 2011-12-02
      • 2014-10-24
      • 2012-05-08
      • 2023-03-16
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多