【问题标题】:How to set align (right or left) of text in TextView, programmatically如何以编程方式在 TextView 中设置文本的对齐(右或左)
【发布时间】:2018-11-05 14:31:06
【问题描述】:
TextView textView=(TextView)customview.findViewById(R.id.textView);

如何让文字左对齐? 我一直在寻找解决方案,但我发现只是在 XML 文件中设置对齐..

【问题讨论】:

  • textView 的父对象是什么?
  • @SushinPv 听起来他们想要对齐父级中的TextView,而不是其中的文本。我就是这个意思。
  • @MikeM。在这个问题中,没有具体说明他是否正在寻找相对于父母的文本对齐方式。听起来他想让文本向左或向右"How to align it left? I was looking for solution but what i found is only setting aligment in XML file.. android "
  • @SushinPv 从那个 ^ 以及他们对您的答案留下的评论中,我认为他们想要像 setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL) 这样的东西。

标签: android alignment


【解决方案1】:

看起来很清楚。

yourTextView.setGravity(Gravity.RIGHT)

【讨论】:

    【解决方案2】:

    您可以在 xml 文件中添加以下代码,并且还存在其他对齐方式。

    <TextView
        android:id="@+id/textViewResponse"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="textEnd"
        android:gravity="end" />
    

    或者您可以在 java 文件中添加以下行,但它需要 API 级别 17。

        textView.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END);
    

    【讨论】:

      【解决方案3】:
       RelativeLayout.LayoutParams textViewLayoutParams = new RelativeLayout.LayoutParams(
                          RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
      
                  // add a rule to align to the left
                  textViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
      
                  // make sure the rule was applied
                  textView.setLayoutParams(textViewLayoutParams);
      

      试试,这对我有用。

      【讨论】:

        猜你喜欢
        • 2018-08-12
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        • 2015-10-07
        • 1970-01-01
        相关资源
        最近更新 更多