简体   繁体   English

在线性布局中对齐ImageView和TextView

[英]Align ImageView and TextView in a Linear Layout

I try to align my ImageView and my TextView but the imageView is higher than my textview. 我尝试将ImageView和TextView对齐,但是imageView高于textview。

Xml : Xml:

           <RelativeLayout
            android:layout_marginTop="8dp"
            android:id="@+id/secondRelative"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/firstRelative"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageNote"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:src="@drawable/rank"
                android:layout_marginEnd="2dp"
                />
            <TextView
                android:id="@+id/note"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:text="Test"
                android:layout_alignBaseline="@id/imageNote"
                />

        </RelativeLayout>

What I get on preview : 我得到的预览:

Android预览

And what I get on my device : 而我在设备上得到的:

不对齐元素

I want to align ImageView and TextView but I don't know how to do. 我想对齐ImageView和TextView,但不知道该怎么做。


Edit : 编辑:

Thanks to @Bob Malooga and @Rethinavel Pillai. 感谢@Bob Malooga和@Rethinavel Pillai。

I used setCompoundDrawables of my TextView and that work perfectly ! 我使用了TextView的setCompoundDrawables,效果很好! (And it's better for perf). (而且对于perf更好)。

Here is my new code : 这是我的新代码:

    <RelativeLayout
        android:id="@+id/relativeDetails"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_weight="1"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
            <TextView
                android:id="@+id/time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                />
            <TextView
                android:id="@+id/note"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:layout_below="@+id/time"
                />
            <TextView
                android:id="@+id/internetNeeded"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:layout_below="@+id/note"
                />
    </RelativeLayout>

To set the drawable in TextView : 要在TextView中设置drawable:

            TextView duree = (TextView) convertView.findViewById(R.id.time);
            int colorG = Color.parseColor("#FFA500");
            Float taille = Utils.convertDpInPx(16, activity);
            /*Convert DP in Pixel
              public static float convertDpInPx(int dp, Activity activity) {
                  Resources r = activity.getResources();
                  float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
                  return px;
              }*/
            Drawable imgChrono = getContext().getResources().getDrawable( R.drawable.chrono );

            imgChrono.setBounds( 0, 0, Math.round(taille), Math.round(taille)); //To change width and height of drawable
            imgChrono.setColorFilter(colorG, PorterDuff.Mode.SRC_ATOP); //To change color of element
            duree.setCompoundDrawables(imgChrono, null, null, null );
            duree.setText(duration);

try this way: 尝试这种方式:

<LinearLayout
        android:layout_marginTop="8dp"
        android:id="@+id/secondRelative"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageNote"
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@drawable/rank"
            android:layout_marginEnd="2dp"
            android:layout_marginRight="2dp"/>
        <TextView
            android:id="@+id/note"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Test"
            android:layout_alignBaseline="@id/imageNote"
        />

    </LinearLayout>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM