简体   繁体   English

居中对齐imageview与以上textview

[英]center align imageview with above textview

I want to center align my imageview with the above textview here's an image of what I'm getting. 我想使我的imageview与上面的textview居中对齐,这是我所得到的图像。

在此处输入图片说明

I need something like this in a relative layout 我在相对布局中需要这样的东西

<TextView
    android:id="@+id/strength"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/hero_class"
    android:textColor="#0095FF"
    android:text="Strength"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/agility"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/hero_class"
    android:layout_centerHorizontal="true"
    android:text="Agility"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#0095FF" />

<TextView
    android:id="@+id/intl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/agility"
    android:layout_alignBottom="@+id/agility"
    android:layout_alignParentRight="true"
    android:text="Intelligence"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#0095FF" />

<ImageView
    android:id="@+id/img_int"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/img_str"
    android:src="@drawable/intelligence" />

<ImageView
    android:id="@+id/img_agi"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/img_int"
    android:layout_centerHorizontal="true"
    android:src="@drawable/agility" />

<ImageView
    android:id="@+id/img_str"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/strength"
    android:layout_alignParentLeft="true"
    android:src="@drawable/strength" />

This is what i have done but the problem is that i can have the middle imageview centered align but the left and right one are with align to the most right and most left not in the center of above textview 这是我所做的,但是问题是我可以使中间的imageview居中对齐,但是左侧和右侧的对齐到最右边,最左边不在textview的中心

In layout.xml you can use this for each Text+Image: 在layout.xml中,您可以将其用于每个Text + Image:

<TextView ...
    android:text="The Text"
    android:drawableBottom="@drawable/icon1" />

You could repeat it into an LinearLayout with the horizontal orientation 您可以将其重复到水平方向的LinearLayout中

You can make three Relative Views each containing a Textview and Imageview posited one below the other with the ImageView in the cater of the parent Relative View. 您可以创建三个相对视图,每个相对视图都包含一个Textview和一个Imageview,一个位于另一个视图之下,另一个与ImageView一起位于父级相对视图的范围内。 Finally put these three within an outer Relative View to position them as you like. 最后,将这三个放置在外部“相对视图”中以根据需要放置它们。

You can use LinearLayout as parent Layout having android:orientation="horizontal" . 您可以将LinearLayout用作具有android:orientation="horizontal"父布局。 and then use three LinearLayout each contains TextView and ImageView having android:orientation="vertical" . 然后使用三个LinearLayout分别包含具有android:orientation="vertical" TextViewImageView

See below code- 见下面的代码-

          <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <TextView
            android:id="@+id/strength"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/hero_class"
            android:textColor="#0095FF"
            android:text="Strength"
            android:textAppearance="?android:attr/textAppearanceMedium" />
        <ImageView
            android:id="@+id/img_agi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/img_int"
            android:layout_centerHorizontal="true"
            android:src="@drawable/agility" />
        </LinearLayout>
     <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    TextView ImageView

    </LinearLayout>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    TextView ImageView

    </LinearLayout>
    </LinearLayout>

尝试创建三个垂直方向的线性布局,每个线性布局将包含具有重心的textview和imageview

Hope this helps: 希望这可以帮助:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@android:color/black" >

<RelativeLayout
    android:id="@+id/reltive1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="TEXT VIEW"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/img1"
        android:layout_width="60dp"
        android:layout_height="80dp"
        android:layout_below="@id/text1"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/reltive1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="TEXT VIEW"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/img2"
        android:layout_width="60dp"
        android:layout_height="80dp"
        android:layout_below="@id/text2"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/reltive1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true" >

    <TextView
        android:id="@+id/text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="TEXT VIEW"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/img3"
        android:layout_width="60dp"
        android:layout_height="80dp"
        android:layout_below="@id/text3"
        android:layout_centerHorizontal="true"
        android:background="@android:color/white" />
</RelativeLayout>

</RelativeLayout>

This is what you want... 这就是你想要的...

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="TextView" />
</LinearLayout>

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

    <ImageView
        android:id="@+id/imgView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imgView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imgView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

Check if your ImageView is centered on your layout with this attribute: 使用以下属性检查ImageView是否位于布局的中心:

android:layout_centerHorizontal="true"

For your TextView, it needs a reference to be aligned above it and also be centered. 对于您的TextView,需要一个参考使其在其上方对齐并居中。 So you need this: 所以你需要这个:

 android:layout_alignParentEnd="@+id/imageView"
 android:layout_alignParentRight="@+id/imageView"
 android:layout_alignParentLeft="true"
 android:layout_alignParentStart="true"
 android:gravity="center"

Create GridView adapter and your done. 创建GridView适配器并完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 textview不对齐imageview下方的中心 - textview not align center below of imageview 将ImageView对齐到RelativeLayout中TextView的中心 - Align ImageView to center of a TextView in RelativeLayout 如何在android中根据上面的imageview对齐textview - how to align textview according to above imageview in android 有没有办法在RelativeLayout中将TextView与ImageView垂直居中对齐 - Is there a way to align TextView vertically center with ImageView in RelativeLayout 如何居中对齐图像视图下方的文本视图作为列表视图中的标题 - How to center align a textview below imageview as a title in a List view 如何将TextView与屏幕中心对齐,并将ImageView粘在屏幕旁边? - How to align TextView to center of the screen, and ImageView to stick next to it? 尽管文本大小,将TextView对齐到ImageView的垂直中心? - Align TextView to vertical center of ImageView despite the text size? 在Textview上方设置ImageView - set ImageView above Textview 具有ImageView和TextView的RelativeLayout-将ImageView下方的TextView对齐,均以编程方式在RelativeLayout中居中 - RelativeLayout with ImageView and TextView - Align TextView below ImageView, both center in RelativeLayout Programatically 在 ConstraintLayout 中将 ImageView 与 TextView 对齐 - Align ImageView with TextView in a ConstraintLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM