In my app, I have a button that creates a TextView with a certain message. How can I set the text alignment of the text to the right of the TextView? I see the attribute in XML, but I don't know how to do it in java.
In Kotlin you can use something like the below,
val textView = TextView(context)
textView.apply {
layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT)
textAlignment = View.TEXT_ALIGNMENT_VIEW_START
}
尝试使用
textView.setGravity(Gravity.RIGHT)
您可以使用设置 textview 的重力
textLabel.setGravity(Gravity.CENTER | Gravity.BOTTOM);
1.) Make sure the width is not set to wrap_content
2.) If the text view is inside another container, set gravity to the container. Remember that gravity is to align text and stuff like that while layout_gravity is used to align views within a parent.
3.) TextAlign aligns text at the beginning, end, left, or right of the container. Gravity is a bit more flexible with Center_Vertical, Center_Horizontal, or Center.
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.