我如何设置文本的位置?

Nik*_*kki 4 android textview

我有一个问题,我想从特定的位置设置文本的位置.例如:我在我的textview中有这个文本:"abcd efgh ijkl mnop qrstu vwxyz"现在我希望这个文本从位置"efgh"而不是"abcd"显示我如何在textview中执行此操作.我想改变文本的位置而不是textview的位置.谢谢

kco*_*ock 6

Nikki,只需在您的TextView中添加marginLeft属性,如下所示:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:text="Testing"
    />
Run Code Online (Sandbox Code Playgroud)

你只需要替换左边距所需的任何值.

编辑:对于代码,您应该能够使用以下内容:

TextView tv = new TextView(this);
LayoutParams lp = new LayoutParams(
    LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom); //use ints here
tv.setLayoutParams(lp);
Run Code Online (Sandbox Code Playgroud)

只是为了它的地狱,这里是如何在一行:)

TextView tv = new TextView(this).setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT).setMargins(left, top, right, bottom));
Run Code Online (Sandbox Code Playgroud)