我想将图像和文本视图水平居中。我添加了android:layout_centerInParent="true"
,因为我使用的是Relative Layout
,但它无效。
<?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="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.l"
tools:showIn="@layout/act_l">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="false"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:layout_alignParentEnd="false"
android:background="@color/switch_thumb_disabled_material_light">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:src="@drawable/icon"
android:id="@+id/imageView"
android:layout_marginTop="87dp"
android:nestedScrollingEnabled="false"
android:layout_alignParentTop="true"
android:layout_alignEnd="@+id/email_editText"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/email_editText"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="45dp"
android:layout_marginBottom="250dp"
android:hint="Email"
android:textColorHint="#f1efef"
android:textColor="#ffffff"
android:minWidth="250dp"
android:gravity="center"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText"
android:layout_alignTop="@+id/email_editText"
android:layout_alignStart="@+id/email_editText"
android:layout_marginTop="93dp" />
</RelativeLayout>
我在relative layout
内使用relative layout
。我确实想要改变布局。保留在relative layout
中,我希望水平和垂直对齐文本字段和图片。
答案 0 :(得分:1)
替换
<?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="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:gravity="center"
android:background="@color/switch_thumb_disabled_material_light"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:adjustViewBounds="true"
android:maxHeight="100dp"
android:maxWidth="100dp"
/>
<EditText
android:id="@+id/email_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:hint="Email"
android:layout_marginTop="50dp"
android:inputType="textEmailAddress"
android:minWidth="250dp"
android:textColor="#ffffff"
android:textColorHint="#f1efef" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignTop="@+id/email_editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:minWidth="250dp"
android:ems="10"
android:textColor="#ffffff"
android:textColorHint="#f1efef"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout>
</RelativeLayout>
答案 1 :(得分:0)
而不是使用android:layout_centerInParent =&#34; true&#34;在EditText和ImageView中,使用android:layout_centerHorizontal =&#34; true&#34;。
答案 2 :(得分:0)
试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
android:layout_marginTop="87dp"
android:adjustViewBounds="true"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:nestedScrollingEnabled="false"
android:src="@drawable/cool"/>
<EditText
android:id="@+id/email_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/imageView"
android:inputType="textEmailAddress"
android:minWidth="250dp" />
</RelativeLayout>
答案 3 :(得分:0)
我会转换为LinearLayout,然后使用父级的 gravity 来处理垂直中心。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="false"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:layout_alignParentEnd="false"
android:background="@color/myColor"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="87dp"
android:adjustViewBounds="true"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:nestedScrollingEnabled="false"
android:src="@drawable/cool"/>
<EditText
android:id="@+id/email_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="45dp"
android:layout_marginBottom="250dp"
android:ems="10"
android:inputType="textEmailAddress"
android:minWidth="250dp"/>
</LinearLayout>
答案 4 :(得分:0)
试试这个
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignTop="@+id/foreground"
android:layout_alignBottom="@id/foreground"
android:layout_alignRight="@id/foreground"
android:layout_alignLeft="@id/foreground"
android:onClick="clickedMe" />
<RelativeLayout
android:id="@id/foreground"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="hello123"
android:textColor="#000"
android:textSize="20dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/button_text"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:src="@drawable/ic_arrow_back_black_24dp" />
</RelativeLayout>
</RelativeLayout>
答案 5 :(得分:0)
我尝试将你的代码放在ImageView中的android:layout_centerHorizontal =“true”,然后按照以下方式编辑EditText并从中工作。请注意,我删除了一些代码,这些代码在我尝试的项目中给了我错误
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:background="@color/switch_thumb_disabled_material_light"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="100dp"
android:maxHeight="100dp"
android:src="@drawable/ai"
android:id="@+id/imageView"
android:layout_marginTop="87dp"
android:nestedScrollingEnabled="false"
android:layout_centerHorizontal="true"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/email_editText"
android:layout_alignParentBottom="true"
android:layout_marginBottom="250dp"
android:hint="Email"
android:textColorHint="#f1efef"
android:textColor="#ffffff"
android:minWidth="250dp"
android:layout_centerHorizontal="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText"
android:layout_alignTop="@+id/email_editText"
android:layout_marginTop="93dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>