Insert ImageView in center in Layout in Android
Insert ImageView in center in Layout in Android
我使用 LinearLayout 和 RelativeLayout 组合创建了一个 4 矩形布局。现在我想在前两个布局的中心插入一个黄色图像(个人资料图像),如下图所示:
我做不到?这是我试过的。这样一开始就正确地设置了 2 个布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darkgreen"
android:orientation="vertical">
<LinearLayout
android:id="@+id/profilenameemailID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".12"
android:orientation="horizontal"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp" >
<RelativeLayout
android:id="@+id/profilelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight=".50"
android:background="@color/light_green"
android:gravity="center" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/namesysmbol"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:visibility="visible" />
<TextView
android:id="@+id/profilenameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:paddingLeft="2dp"
android:text="@string/defaultname"
android:textColor="@color/nametext"
android:textSize="14sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/emailID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight=".50"
android:background="@color/light_green"
android:gravity="center" >
<ImageView
android:id="@+id/eicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/emailsymbol"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:visibility="visible" />
<TextView
android:id="@+id/emailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/sicon"
android:paddingLeft="2dp"
android:text="@string/defaultEmail"
android:textColor="@color/emailtext"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
谁能帮我把上面格式的黄色 imageView 插入中间。
谢谢!
你能试试用这个吗Layout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bb4"
android:layout_weight="1"
android:layout_margin="4dp">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#bb4"
android:layout_margin="4dp">
</LinearLayout>
</LinearLayout>
<!-- Your ImageView here!-->
<View
android:layout_centerInParent="true"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#ff5"/>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="170dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bb4"
android:layout_weight="1"
android:layout_margin="4dp">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#bb4"
android:layout_margin="4dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是伪代码,但可以帮助您理解概念
<LinearLayout orientation= vertical>
<FrameLayout>
<LinearLayout orientation= horizontal>
// make to rectangle here
</LinearLayout>
<Image/> create rounded image here with layout_gravity center
</FrameLayout>
<FrameLayout>
<LinearLayout orientation= horizontal>
// make to rectangle here
</LinearLayout>
</FrameLayout>
</LinearLayout>
复制并粘贴到您的 xml 文件中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E3E3E3" >
<RelativeLayout
android:id="@+id/r_1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<View
android:id="@+id/view1"
android:layout_width="5dp"
android:layout_height="1dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/view1"
android:background="#666" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/view1"
android:background="#666" >
</RelativeLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#ff00ff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@+id/r_1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<View
android:id="@+id/view2"
android:layout_width="5dp"
android:layout_height="1dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/view2"
android:background="#666" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/view2"
android:background="#666" >
</RelativeLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#ff00ff" />
</RelativeLayout>
我使用 LinearLayout 和 RelativeLayout 组合创建了一个 4 矩形布局。现在我想在前两个布局的中心插入一个黄色图像(个人资料图像),如下图所示:
我做不到?这是我试过的。这样一开始就正确地设置了 2 个布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darkgreen"
android:orientation="vertical">
<LinearLayout
android:id="@+id/profilenameemailID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".12"
android:orientation="horizontal"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="2dp" >
<RelativeLayout
android:id="@+id/profilelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight=".50"
android:background="@color/light_green"
android:gravity="center" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/namesysmbol"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:visibility="visible" />
<TextView
android:id="@+id/profilenameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:paddingLeft="2dp"
android:text="@string/defaultname"
android:textColor="@color/nametext"
android:textSize="14sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/emailID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:layout_weight=".50"
android:background="@color/light_green"
android:gravity="center" >
<ImageView
android:id="@+id/eicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/emailsymbol"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:visibility="visible" />
<TextView
android:id="@+id/emailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/sicon"
android:paddingLeft="2dp"
android:text="@string/defaultEmail"
android:textColor="@color/emailtext"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
谁能帮我把上面格式的黄色 imageView 插入中间。
谢谢!
你能试试用这个吗Layout
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="170dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bb4"
android:layout_weight="1"
android:layout_margin="4dp">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#bb4"
android:layout_margin="4dp">
</LinearLayout>
</LinearLayout>
<!-- Your ImageView here!-->
<View
android:layout_centerInParent="true"
android:layout_width="90dp"
android:layout_height="90dp"
android:background="#ff5"/>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="170dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bb4"
android:layout_weight="1"
android:layout_margin="4dp">
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#bb4"
android:layout_margin="4dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是伪代码,但可以帮助您理解概念
<LinearLayout orientation= vertical>
<FrameLayout>
<LinearLayout orientation= horizontal>
// make to rectangle here
</LinearLayout>
<Image/> create rounded image here with layout_gravity center
</FrameLayout>
<FrameLayout>
<LinearLayout orientation= horizontal>
// make to rectangle here
</LinearLayout>
</FrameLayout>
</LinearLayout>
复制并粘贴到您的 xml 文件中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E3E3E3" >
<RelativeLayout
android:id="@+id/r_1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<View
android:id="@+id/view1"
android:layout_width="5dp"
android:layout_height="1dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/view1"
android:background="#666" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/view1"
android:background="#666" >
</RelativeLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#ff00ff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="@+id/r_1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<View
android:id="@+id/view2"
android:layout_width="5dp"
android:layout_height="1dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/view2"
android:background="#666" >
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/view2"
android:background="#666" >
</RelativeLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#ff00ff" />
</RelativeLayout>