-1

As the title description, when set layout_gravity for TextView in LinearLayout, this property does not work well, and when I set it through LinearLayout.LayoutParams.gravity, it does not work as well. But for ImageView with same way, it works.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.openssl.LayoutTestActivity">
    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="parent"
        android:background="@color/colorAccent">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="xml"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:src="@drawable/ic_launcher_round"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

Java code

 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
        TextView textView = new TextView(this);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
            .LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.setMargins(10, 0, 10, 15);
        textView.setLayoutParams(layoutParams);
        textView.setText("Code");
        linearLayout.addView(textView);
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(R.drawable.ic_launcher_round);
        LinearLayout.LayoutParams layoutParams1 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
            .LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 0, 10, 15);
        layoutParams1.gravity = Gravity.BOTTOM;
        imageView.setLayoutParams(layoutParams1);
        linearLayout.addView(imageView);

enter image description here

Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
wqycsu
  • 290
  • 2
  • 16

1 Answers1

0

in the TextView you are creating programmatically, you are giving the TextView a 15dp bottom margin, If you want to make the TextView itself at the bottom, set the gravity of the layoutParams without giving the view unnecessary margins:

layoutParams.setMargins(10, 0, 10, 0);

And talking about the textView in xml, if you want to align the text itself of the textView to be at the bottom, set the height to match_parent and set view gravity to bottom:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:gravity="bottom"
    android:text="xml"/>

for the TextViewin the code, try to set the height to match_parent and give the view a gravity of BOTTOM inside the layoutParams :

LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
        .LayoutParams.MATCH_PARENT, Gravity.BOTTOM);
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • It works for `TextView` in `xml`, but although I cancel the bottom margin, the `TextView` in the code does not work as expected. And I want to know why this phenomenon happens, could you do me a favor? – wqycsu Jan 18 '18 at 01:38
  • I can wrap the `TextView` with a `ViewGroup` to solve this problem as well. But I'm very confused about that the `layout_gravity` property does not work for `TextView`. – wqycsu Jan 18 '18 at 01:52