0

我有一个Activity被调用的MainActivity.java,带有一个activity_main.xml文件。但是当我尝试添加第二行时,我不知道如何创建多行文本视图,它将两行放在同一行的同一空间。

activity_main.xml 文本视图

<TextView  android:id="@+id/txtView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:textSize="12sp"
    android:textColorLink="#FFFF00"
    android:textStyle="bold"
/
<TextView  android:id="@+id/txtView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:textSize="12sp"
    android:textStyle="bold"
/>

MainActivity.java

public class MainMenuActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.alatecinc.eema.MESSAGE";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    //Main Menu Links
    String str = "Directions";
    SpannableString content = new SpannableString(str);
    content.setSpan(new UnderlineSpan(), 0, str.length(), 0);
    final TextView txtView = (TextView) findViewById(R.id.txtView);
    txtView.setText(content);
    txtView.setTextColor(Color.BLUE);
    Linkify.addLinks(txtView, Linkify.ALL);     
    txtView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            MainMenuActivity.this.startActivity(new Intent(MainMenuActivity.this, DirectionActivity.class));
        }
    });

    String mapStr = "Map View";
    SpannableString mapContent = new SpannableString(mapStr);
    mapContent.setSpan(new UnderlineSpan(), 0, mapStr.length(), 0);
    final TextView mapTxtView = (TextView) findViewById(R.id.txtView1);
    mapTxtView.setText(mapContent);
    mapTxtView.setTextColor(Color.BLUE);
    Linkify.addLinks(mapTxtView, Linkify.ALL);      
    mapTxtView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            MainMenuActivity.this.startActivity(new Intent(MainMenuActivity.this, DirectionActivity.class));
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main_menu, menu);
          return true;
     }

 }

所以我需要的是有两条线,一条是方向,另一条是地图视图。

4

6 回答 6

2

使用相对布局并将 txtView2 显式设置为低于 txtView1。我不会选择 HTML 解决方案,因为它违背了拥有单独布局文件的目的。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relativeLayout1">
    <TextView android:id="@+id/txtView1"
        android:text=""
        android:textSize="12sp"
        android:textStyle="bold"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/txtView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" 
        android:textSize="12sp"
        android:textStyle="bold" 
        android:layout_below="@id/txtView1"/>

于 2013-03-13T14:18:45.587 回答
1

试试这个:

<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"
     tools:context=".yourActivity" >
<TextView  android:id="@+id/txtView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="" 
    android:textSize="12sp"
    android:textColorLink="#FFFF00"
    android:textStyle="bold"/>
<TextView  android:id="@+id/txtView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txtView"
    android:text="" 
    android:textSize="12sp"
    android:textStyle="bold"/>
</RelativeLayout>
于 2013-03-13T14:16:01.107 回答
0

试试这个,

yourTextView.setText(Html.fromHtml("<div>line1</div> <br /> <div>line2</div>"));
于 2013-03-13T14:13:35.573 回答
0

\n当你想添加新行时使用它。像

String str = "Title:\nHere title";
于 2013-03-13T14:18:29.407 回答
0

您需要在 strings.xml 文件中定义您的文本。是的,您也可以在其中定义格式化的 html。

所以你可以试试这个:

<string name="nice_html">
<![CDATA[Don&apos;t worry we will never share this with anyone<br/>
By Clicking register you are indicating that you have read and agreed to the <br/>
 <u>Terms of services</u>  and <u>Privacy Policy</u>
]]></string>

然后,在您的代码中:

TextView foo = (TextView)findViewById(R.id.foo); foo.setText(Html.fromHtml(getString(R.string.nice_html)));

原始答案。

这是最干净的方法。Inshallah。

于 2013-03-13T14:32:48.477 回答
0

您可以在 xml 文件的 textview 中添加 android:maxLines="5" 属性。

<TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:maxLines="5"
        android:textAppearance="?android:attr/textAppearanceLarge" />

就是这样

于 2015-09-07T06:03:03.160 回答