简体   繁体   中英

Extra unwanted margin of TextViews _ android studio

I'm designing this XML file:

<android.support.constraint.ConstraintLayout
                android:id="@+id/title_layout"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:background="#00C7C7">


                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    app:srcCompat="@drawable/icon_n"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginTop="16dp"
                    />

                <TextView
                    android:id="@+id/list_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@id/imageView"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:text="@string/all"
                    android:fontFamily="@font/koohinoor_bold"
                    android:textSize="30sp"
                    android:textColor="#FFFFFF"
                    />

                <TextView
                    android:id="@+id/description"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@id/list_title"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    android:text="@string/all_alphabetic_order"
                    android:fontFamily="@font/koohinoor_bold"
                    android:textSize="13sp"
                    android:textColor="#3A3939"
                    android:layout_marginBottom="16dp"
                    />
</android.support.constraint.ConstraintLayout>

A TextView is located under the ImageView . Also another TextView is located under the first-mentioned TextView . I expect there be no margins between the TextView s and the ImagView , but there are extra unwanted margins...

what could be the problem? what could be the solution?


notes:

I'm using Persian fonts.

In XML preview, no problem is observed. This issue is seen on run-time.

you need to add vertical bias to your textview ( description )

  <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/list_title"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:text="@string/all_alphabetic_order"
                android:fontFamily="@font/koohinoor_bold"
                android:textSize="13sp"
                android:textColor="#3A3939"
                android:layout_marginBottom="16dp"
                app:layout_constraintVertical_bias="0" 
                />

Add android:includeFontPadding="false" to your TextViews , and your done!

First off all you can use LinearLayout for your situation. It is more effective. Also in LinearLayout you can use negative margins which you need in your case. Try this layout.

<LinearLayout
        android:id="@+id/title_layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#00C7C7"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginTop="16dp"
            android:src="@drawable/ic_signal_chain_power" />

        <TextView
            android:id="@+id/list_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="-4dp"
            android:fontFamily="@font/koohinoor_bold"
            android:text="@string/all"
            android:textColor="#FFFFFF"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="-4dp"
            android:layout_marginBottom="16dp"
            android:fontFamily="@font/koohinoor_bold"
            android:text="@string/all_alphabetic_order"
            android:textColor="#3A3939"
            android:textSize="13sp" />
    </LinearLayout>

set imageview height to wrap-content

 <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="80dp"
                    android:layout_height="wrap-content"
                    app:srcCompat="@drawable/icon_n"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    android:layout_marginTop="16dp"
                    />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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