无法在代码中更改TextView的宽度

时间:2014-09-16 06:59:05

标签: android textview

我想要做的是......我希望我的TextView位于屏幕右侧,如“Note Edge”屏幕。

我试过的是那个

这是 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<TextView
    android:text="@string/hello_world"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:layout_alignParentRight="true"
    android:id="@+id/txtVertical" />

</RelativeLayout>

这是 Java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    TextView txtVertical = (TextView)findViewById(R.id.txtVertical);
    txtVertical.setBackgroundColor(Color.CYAN);
    txtVertical.setPivotX(0);
    txtVertical.setPivotY(0);
    txtVertical.setRotation(90f);

    txtVertical.setX(100f);
    txtVertical.setY(100f);

    txtVertical.setText(txtVertical.getText() + "                                        ");
    txtVertical.setSelected(true);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);

    //txtVertical.setWidth(size.y);              //doesn't work
    txtVertical.getLayoutParams().width = 1000;  //doesn't work also
    txtVertical.requestLayout();

    Log.d("size.y", size.y + "");

}

如果我设置属性android:layout_width="wrap_content",它也不起作用。如何更改width的{​​{1}}?

5 个答案:

答案 0 :(得分:1)

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/txtVertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"
        android:text="@string/hello_world" />

</RelativeLayout>

答案 1 :(得分:0)

希望这可以帮助你

    LayoutParams params = textView.getLayoutParams();
    params.height = 70;
    params.width= 50;
    textView.setLayoutParams(params);

答案 2 :(得分:0)

试试;

  

txtVertical.getLayoutParams()。width = 200

(以像素为单位)

答案 3 :(得分:0)

只需将这些layoutparams添加到TextView 以编程方式

// Declaring the TextView LayoutParams
    LinearLayout.LayoutParams text_lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
    text_lp.gravity = Gravity.CENTER;

// Declaring the TextView itself
    TextView txtVertical = (TextView)findViewById(R.id.txtVertical);
    txtVertical.setLayoutParams(text_lp);
    txtVertical.setGravity(Gravity.RIGHT);
    txtVertical.setSingleLine();
    txtVertical.setEllipsize(TruncateAt.MARQUEE);
    txtVertical.setSelected(true);
    //complete text your customization

答案 4 :(得分:0)

试试这种方式

TextView txtVertical = (TextView)findViewById(R.id.txtVertical);
txtVertical.requestLayout();
txtVertical.getLayoutParams().height = 230;
txtVertical.getLayoutParams().width = 400;
txtVertical.setPadding(6, 6, 6, 6);