在另一个视图的中心对齐视图
Align view in the center of another view
我有这样的布局:
我用这段代码创建的:
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/BusinessLogoCircleImageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="36dp"
android:layout_marginBottom="36dp"
android:layout_gravity="right|end"
android:background="#ff4fc3f7" />
<LinearLayout
android:layout_toRightOf="@+id/BusinessLogoCircleImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessNameTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessAddressTextView"
android:layout_below="@+id/BusinessNameTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessWebsiteTextView"
android:layout_below="@+id/BusinessAddressTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
</LinearLayout>
<TextView
android:text="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CommentsCountTextView"
android:layout_below="@+id/BusinessLogoCircleImageView"
android:gravity="center"
android:background="@color/primary_color"
android:textSize="@dimen/Counter_Default_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="overall thanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/CommentsCountTextView"
android:id="@+id/CommentsTextTextView"
android:gravity="center"
android:padding="3dp"
android:textSize="@dimen/SubCounter_Text_size"
android:textColor="@color/white"
android:background="@color/subtitle_color"
android:typeface="normal" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/CommentsTextTextView"
android:id="@+id/StaffTeamListView" />
<TextView
android:id="@+id/emptyMessagesListTextView"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center" />
问题是我希望 3 textview 位于左侧 imageview 的垂直中心。但我不知道该怎么做,我尝试使用相对布局并将这些 textview 包装在 linearlayout
但问题总是一样的,如何设置线性布局使内容根据图像居中?如果我可以在没有线性布局的情况下做到这一点,我会更喜欢它,同时请记住,我想在每个文本视图的左侧显示一个小图像(图标)。
我想到的一个解决方案是,如果我将 imageview 和 linearlayout 与 textviews 放在另一个 linearlayout 中,然后对齐,然后居中垂直,但这是最好的解决方案吗?
这是一些 "pseudo" 代码:
linearlayout orientation=horizontal, width=wrap_content
imageview
linearlayout orientation=vertial, width=0dp, weight=1
textview1
textview2
textview 3
权重为 1 会给文本视图的父级剩余 space。
一种解决方案是将图像和 3 个文本包装在 RelativeLayout 中。
相对布局的高度由包裹的内容给出,而其中的项目垂直居中,这意味着图像或 3 行文本的高度可以变化,而另一行将居中.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/BusinessLogoCircleImageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="36dp"
android:layout_marginBottom="36dp"
android:layout_gravity="right|end"
android:layout_centerVertical="true"
android:background="#ff4fc3f7" />
<LinearLayout
android:layout_toRightOf="@+id/BusinessLogoCircleImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:layout_centerVertical="true">
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessNameTextView"
android:background="#00FF00"
android:textSize="14dp"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessAddressTextView"
android:layout_below="@+id/BusinessNameTextView"
android:background="#00FF00"
android:textSize="14dp"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessWebsiteTextView"
android:layout_below="@+id/BusinessAddressTextView"
android:background="#00FF00"
android:textSize="14dp"
android:textColor="@color/white"
android:typeface="normal" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.maks.myapplication.MainActivity">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/BusinessLogoCircleImageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="36dp"
android:layout_marginBottom="36dp"
android:layout_gravity="right|end"
android:background="#ff4fc3f7" />
<LinearLayout
android:layout_toRightOf="@+id/BusinessLogoCircleImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessNameTextView"
android:typeface="normal" />
<TextView
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessAddressTextView"
android:layout_below="@+id/BusinessNameTextView"
android:typeface="normal" />
<TextView
android:text="Website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessWebsiteTextView"
android:layout_below="@+id/BusinessAddressTextView"
android:typeface="normal" />
</LinearLayout>
</LinearLayout>
<TextView
android:text="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CommentsCountTextView"
android:layout_below="@+id/linear"
android:gravity="center"
android:typeface="normal" />
<TextView
android:text="overall thanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/CommentsCountTextView"
android:id="@+id/CommentsTextTextView"
android:gravity="center"
android:padding="3dp"
android:typeface="normal" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/CommentsTextTextView"
android:id="@+id/StaffTeamListView" />
<TextView
android:id="@+id/emptyMessagesListTextView"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center" />
</RelativeLayout>
用这个。希望对你有帮助。
我有这样的布局:
我用这段代码创建的:
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/BusinessLogoCircleImageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="36dp"
android:layout_marginBottom="36dp"
android:layout_gravity="right|end"
android:background="#ff4fc3f7" />
<LinearLayout
android:layout_toRightOf="@+id/BusinessLogoCircleImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessNameTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessAddressTextView"
android:layout_below="@+id/BusinessNameTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessWebsiteTextView"
android:layout_below="@+id/BusinessAddressTextView"
android:background="@color/primary_color"
android:textSize="@dimen/Address_Text_size"
android:textColor="@color/white"
android:typeface="normal" />
</LinearLayout>
<TextView
android:text="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CommentsCountTextView"
android:layout_below="@+id/BusinessLogoCircleImageView"
android:gravity="center"
android:background="@color/primary_color"
android:textSize="@dimen/Counter_Default_size"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="overall thanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/CommentsCountTextView"
android:id="@+id/CommentsTextTextView"
android:gravity="center"
android:padding="3dp"
android:textSize="@dimen/SubCounter_Text_size"
android:textColor="@color/white"
android:background="@color/subtitle_color"
android:typeface="normal" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/CommentsTextTextView"
android:id="@+id/StaffTeamListView" />
<TextView
android:id="@+id/emptyMessagesListTextView"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center" />
问题是我希望 3 textview 位于左侧 imageview 的垂直中心。但我不知道该怎么做,我尝试使用相对布局并将这些 textview 包装在 linearlayout
但问题总是一样的,如何设置线性布局使内容根据图像居中?如果我可以在没有线性布局的情况下做到这一点,我会更喜欢它,同时请记住,我想在每个文本视图的左侧显示一个小图像(图标)。
我想到的一个解决方案是,如果我将 imageview 和 linearlayout 与 textviews 放在另一个 linearlayout 中,然后对齐,然后居中垂直,但这是最好的解决方案吗?
这是一些 "pseudo" 代码:
linearlayout orientation=horizontal, width=wrap_content
imageview
linearlayout orientation=vertial, width=0dp, weight=1
textview1
textview2
textview 3
权重为 1 会给文本视图的父级剩余 space。
一种解决方案是将图像和 3 个文本包装在 RelativeLayout 中。
相对布局的高度由包裹的内容给出,而其中的项目垂直居中,这意味着图像或 3 行文本的高度可以变化,而另一行将居中.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/BusinessLogoCircleImageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="36dp"
android:layout_marginBottom="36dp"
android:layout_gravity="right|end"
android:layout_centerVertical="true"
android:background="#ff4fc3f7" />
<LinearLayout
android:layout_toRightOf="@+id/BusinessLogoCircleImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:layout_centerVertical="true">
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessNameTextView"
android:background="#00FF00"
android:textSize="14dp"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessAddressTextView"
android:layout_below="@+id/BusinessNameTextView"
android:background="#00FF00"
android:textSize="14dp"
android:textColor="@color/white"
android:typeface="normal" />
<TextView
android:text="Website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessWebsiteTextView"
android:layout_below="@+id/BusinessAddressTextView"
android:background="#00FF00"
android:textSize="14dp"
android:textColor="@color/white"
android:typeface="normal" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.maks.myapplication.MainActivity">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="48dp"
android:layout_height="48dp"
android:id="@+id/BusinessLogoCircleImageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="36dp"
android:layout_marginBottom="36dp"
android:layout_gravity="right|end"
android:background="#ff4fc3f7" />
<LinearLayout
android:layout_toRightOf="@+id/BusinessLogoCircleImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessNameTextView"
android:typeface="normal" />
<TextView
android:text="Address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessAddressTextView"
android:layout_below="@+id/BusinessNameTextView"
android:typeface="normal" />
<TextView
android:text="Website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/BusinessWebsiteTextView"
android:layout_below="@+id/BusinessAddressTextView"
android:typeface="normal" />
</LinearLayout>
</LinearLayout>
<TextView
android:text="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/CommentsCountTextView"
android:layout_below="@+id/linear"
android:gravity="center"
android:typeface="normal" />
<TextView
android:text="overall thanks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/CommentsCountTextView"
android:id="@+id/CommentsTextTextView"
android:gravity="center"
android:padding="3dp"
android:typeface="normal" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/CommentsTextTextView"
android:id="@+id/StaffTeamListView" />
<TextView
android:id="@+id/emptyMessagesListTextView"
android:text=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center" />
</RelativeLayout>
用这个。希望对你有帮助。