Android 为什么布局不适用于TextView?
作为标题说明,当在Android 为什么布局不适用于TextView?,android,android-layout,textview,layout-gravity,Android,Android Layout,Textview,Layout Gravity,作为标题说明,当在LinearLayout中为TextView设置layout\u gravity时,此属性不起作用,当我通过LinearLayout.LayoutParams.gravity进行设置时,它也不起作用。但对于ImageView来说,同样的方法也可以 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http:
LinearLayout
中为TextView
设置layout\u gravity
时,此属性不起作用,当我通过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>
在以编程方式创建的文本视图中,您为文本视图提供了
15dp
底部边距,如果您想使TextView
本身位于底部,请设置布局参数的重力,而不给视图不必要的边距:
layoutParams.setMargins(10, 0, 10, 0);
谈到xml中的textView
,如果要将textView
的文本本身对齐到底部,请将高度设置为匹配父视图,并将视图重力设置为底部:
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="xml"/>
它适用于
xml
中的TextView
,但尽管我取消了底部边距,但代码中的TextView
并没有按预期工作。我想知道为什么会出现这种现象,你能帮我一个忙吗?我可以用ViewGroup
包装TextView
来解决这个问题。但是我非常困惑的是,layout\u gravity
属性不适用于TextView
。
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:gravity="bottom"
android:text="xml"/>
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup
.LayoutParams.MATCH_PARENT, Gravity.BOTTOM);