为什么layout_gravity不适用于TextView?

时间:2018-01-17 12:03:59

标签: android android-layout textview layout-gravity

作为标题说明,在layout_gravity中为TextView设置LinearLayout时,此属性效果不佳,当我通过LinearLayout.LayoutParams.gravity设置时,它不会也工作。但是对于ImageView以同样的方式,它可以工作。

<?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代码

 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

1 个答案:

答案 0 :(得分:0)

在您以编程方式创建的TextView中,您为TextView提供了15dp底部边距,如果您想在底部设置TextView,请设置layoutParams的重力而不给出查看不必要的边距:

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

在xml中讨论textView时,如果要将textView的文本本身对齐到底部,请将高度设置为match_parent并将视图重力设置为底部:

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

代码中的TextView,尝试将高度设置为match_parent,并在BOTTOM内为视图提供layoutParams的重力:

LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
        .LayoutParams.MATCH_PARENT, Gravity.BOTTOM);
相关问题