我可以找到如何将句子限制为我的java代码中定义的单行。我的文字是通过意图传递的。这就是我正在尝试的东西 -
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
String message=intent.getStringExtra(MyActivity.EXTRA_VALUE);
TextView text=new TextView(this);
text.setTextSize(40);
text.setText(message);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.RIGHT_OF,R.id.imageView);
p.addRule(RelativeLayout.ALIGN_TOP);
RelativeLayout layout=(RelativeLayout) findViewById(R.id.content);
text.setLayoutParams(p);
layout.addView(text);
}
我对应的xml文件是 -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="shanky.thefirst.DisplayMessageActivity"
tools:showIn="@layout/activity_display_message">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="@+id/imageView"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
我的输出是 -
欢迎
的Vivek!
代替 - 欢迎Vivek!
答案 0 :(得分:1)
这就是你想要的。只是做:
text.setSingleLine();
它会将您的textview设置为单行