代码之家  ›  专栏  ›  技术社区  ›  Ajay J G

Android中如何将图标垂直对齐到textview第一行的中心

  •  0
  • Ajay J G  · 技术社区  · 5 年前

    但那没用。有人能帮我吗?

     <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp">
    
        <ImageView
            android:id="@+id/iv_itm_tick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_tick_mark"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            app:layout_constraintStart_toEndOf="@+id/iv_itm_tick"
            app:layout_constraintTop_toTopOf="parent"
            android:text="Concepts of deep learning and machine learning workflow" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  2
  •   Cheticamp    5 年前

    如果 图片框 是一个 您可以简单地将其基线约束到另一个的基线 文本框 . 不幸的是, 图像视图 没有一个可行的基线概念,所以不能简单地约束 图片框 文本框 . (试试看会发生什么。)

    图片框 用一个 文本框 ,将图标设置为

    滴答声_背景.xml

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@drawable/ic_tick_mark"
            android:gravity="center" />
    </layer-list>
    

    记号可以如下所示:

    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:tint="#66A8DD"
        android:viewportWidth="24"
        android:viewportHeight="24">
        <path
            android:fillColor="#FFFFFFFF"
            android:pathData="M9,16.17L4.83,12l-1.42,1.41L9,19 21,7l-1.41,-1.41z" />
    </vector>
    

    如果底层图标是位图,则层列表也可以是可绘制的XML位图。

    另一种方法也是可行的,就是创建一个空的 具有相同的特征 要将记号标记附加到。约束空 到另一个的基线 文本框 .

    现在,坐飞机 图片框 并将其顶部和底部约束为空 文本框 . 刻度线将按需要排列。

    这是一个只支持XML的解决方案。其他解决方案可能涉及一些编码。

    enter image description here

    <androidx.constraintlayout.widget.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        tools:context=".MainActivity">
    
        <ImageView
            android:id="@+id/iv_itm_tick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_tick_mark"
            app:layout_constraintBottom_toBottomOf="@id/dummyView"
            app:layout_constraintEnd_toStartOf="@id/textView"
            app:layout_constraintTop_toTopOf="@id/dummyView" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:text="Concepts of deep learning and machine learning workflow"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/dummyView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            app:layout_constraintBaseline_toBaselineOf="@id/textView"
            app:layout_constraintEnd_toStartOf="@+id/textView"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
        2
  •  -1
  •   sajjad Ariel Di Miro    5 年前

    按以下方式编辑xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp">
    
    <ImageView
        android:id="@+id/iv_itm_tick"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_tick_mark"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="Concepts of deep learning and machine learning workflow"
        app:layout_constraintStart_toEndOf="@+id/iv_itm_tick"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>