【问题标题】:How to change postion of textview in java the way i would in xml如何以我在 xml 中的方式更改 java 中 textview 的位置
【发布时间】:2013-07-24 11:11:17
【问题描述】:

在 xml 中,我的 textview 看起来像这样。

`

     android:layout_marginTop="248dp"
     android:text="hello world"
     android:textColor="#646464"
     android:textSize="200sp"
     android:textStyle="bold" />`

当我想向左或向右移动我的文本视图时,我编辑 marginleft 参数中的值,如果我想向上或向下移动我的文本视图,我编辑边距顶部值。我希望能够对 java 中的 textview 定位进行这种控制。有人可以向我展示一个正在完成的代码示例,可能使用我的 textview 作为示例并移动 textview 以使其新的剩余边距为 160?

【问题讨论】:

    标签: java android xml android-layout android-xml


    【解决方案1】:

    here

    创建布局参数并设置边距 即layoutParams.setMargins(160,0,0,0);

    【讨论】:

      【解决方案2】:

      如果您想以编程方式更改TextView 的边距(in dp),请尝试此操作(将 left、top、right、bottom 的值更改为您要设置的值作为边距):

      float density = getResources().getDisplayMetrics().density;
      RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) textView.getLayoutParams();
      params.setMargins(left * density, top * density, right * density, bottom * density);
      textView.setLayoutParams(params);
      

      【讨论】:

        【解决方案3】:

        您可以通过编程方式更改 TextView 的 LayoutParams,如下所示:

        TextView textView = /* your textview here ... */;
        LinearLayout.LayoutParams lparam = 
               new LinearLayout.LayoutParams(textView.getLayoutParams());
        lparam.topMargin = /* ... */;
        lparam.rightMargin = /* ... */;
        // or use lparam.setMargins() method.
        textView.setLayoutParams(p);
        

        【讨论】:

          【解决方案4】:
          TextView tv =(TextView)findViewById(R.id.id of textview in xml);
          
           tv.setPadding(15, 0, 0, 0);
          //tv.setPadding(left, top, right, bottom);
           tv.setText( "Work");
           tv.setTextSize(17);
           tv.setTextColor(Color.parseColor("#000000"));
          

          您可以在 java 代码中执行此操作。

          【讨论】:

          • -1 padding 改变其内容的起始位置,而不是改变自身的位置
          猜你喜欢
          • 2022-12-13
          • 1970-01-01
          • 2012-12-24
          • 1970-01-01
          • 2013-03-13
          • 1970-01-01
          • 1970-01-01
          • 2015-04-05
          • 1970-01-01
          相关资源
          最近更新 更多