【问题标题】:Arrange imageview in the middle of top layout将 imageview 排列在顶部布局的中间
【发布时间】:2018-11-03 04:07:19
【问题描述】:

我有这样一个带有自定义顶部标题的布局。我希望ImageView 位于标题的中间,即center_horizo​​ntal。我尝试为布局设置布局重力和重力,但有时还可以。但是,当我在横向模式下尝试应用程序时,徽标转到一侧,并且图像不在布局的正确中心。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/phoneoptLayout"
android:background="@mipmap/bgg"
android:paddingBottom="5dp"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="6dp">

<LinearLayout
    android:id="@+id/top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/back"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="left"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:paddingTop="10dp"
        android:src="@drawable/arrow_back" />

    <ImageView android:src="@mipmap/logo"
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:layout_marginRight="30dp"
        android:layout_gravity="center_horizontal" />

</LinearLayout>
</LinearLayout>

在另一种布局中,标题右侧有一个退出按钮。这也不在中心。我是 android 新手,所以我不知道如何解决这个问题。

【问题讨论】:

  • 我认为您的标签以RelativeLayout 开头并以Linearlayout 结尾。什么是实际使用必须使用?
  • 你能把页面截图添加到横向吗?

标签: android positioning android-relativelayout


【解决方案1】:

我希望这对你有用。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/phoneoptLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="6dp">

    <ImageView
        android:id="@+id/back"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="10dp"
        android:src="@mipmap/ic_launcher" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="55dp"
        android:layout_centerInParent="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginRight="30dp"
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>

【讨论】:

  • 横屏还是一样。 ://
【解决方案2】:

用相对布局替换你的线性布局。你也有顶级相对布局,但你有封闭的线性布局。

【讨论】:

    【解决方案3】:

    您在 LinearLayout 中使用了android:layout_centerHorizontal="true"。你会得到你的 LinearLayout 在顶部布局的中间。

    例如:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/phoneoptLayout"
        android:background="@mipmap/ic_launcher"
        android:paddingBottom="5dp"
        android:orientation="vertical"
        android:paddingLeft="5dp"
        android:paddingRight="6dp">
    
        <LinearLayout
            android:id="@+id/top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal">
    
            <ImageView
                android:id="@+id/back"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="left"
                android:layout_marginLeft="5dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="10dp"
                android:paddingTop="10dp"
                android:src="@drawable/send_video" />
    
            <ImageView android:src="@mipmap/ic_launcher"
                android:layout_width="wrap_content"
                android:layout_height="55dp"
                android:paddingBottom="5dp"
                android:paddingTop="5dp"
                android:layout_marginRight="30dp"
                android:layout_gravity="center_horizontal" />
    
        </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      做你的线性布局宽度“match_parent” 像这样

       <LinearLayout
          android:id="@+id/top"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
      

      【讨论】:

        【解决方案5】:

        使 LinearLayout 的高度和宽度与父级和重心相匹配

            <LinearLayout
             android:id="@+id/top"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:orientation="horizontal"
             android:layout_gravity="center">
        

        【讨论】:

          【解决方案6】:

          你可以试试下面的代码。

          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@+id/phoneoptLayout"
          android:background="@mipmap/bgg">
          
              <RelativeLayout
                  android:id="@+id/top"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
                  <ImageView
                      android:id="@+id/back"
                      android:layout_width="50dp"
                      android:layout_height="50dp"
                      android:layout_gravity="left"
                      android:layout_centerVertical="true"
                      android:src="@drawable/arrow_back" />
          
                  <ImageView 
                      android:src="@mipmap/logo"
                      android:layout_width="wrap_content"
                      android:layout_height="55dp"
                      android:layout_centerHorizontal="true"
                      android:layout_centerVertical="true" />
          
              </RelativeLayout>
          </RelativeLayout>
          

          【讨论】:

            猜你喜欢
            • 2017-03-07
            • 1970-01-01
            • 2017-06-09
            • 2015-04-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-21
            • 2012-03-22
            相关资源
            最近更新 更多