eclipse 如何显示两个相邻的文本视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17112308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:44:21  来源:igfitidea点击:

How do I show two text views next to each other

javaandroideclipsetextviewandroid-sdk-2.3

提问by yams

I have a Main Activity That has a Clickable Link that launches a Activity. Right now what is showing is "Click Here" in a text view. I would like to change it to : "To see Evacuation Routes - Click Here" With the "Click Here" being the underlined text. Below is the Code I currently have.

我有一个主活动,它有一个启动活动的可点击链接。现在显示的是文本视图中的“单击此处”。我想将其更改为:“查看疏散路线 - 单击此处”,其中带下划线的文本为“单击此处”。以下是我目前拥有的代码。

activity_main.xml

活动_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
<TextView  android:id="@+id/txtView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txtView_5"
    android:text="" 
    android:textSize="20sp"
    android:textStyle="bold"/>
</RelativeLayout>

MainActivity.java

主活动.java

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

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    AsyncRouteNames asyncRouteNames = new AsyncRouteNames();
    asyncRouteNames.execute();




    String mapStr = "Click Here";
    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);      
    ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isMobile = false;
    if (activeNetwork != null && activeNetwork.isConnected()){
        isMobile = true;
    }

    if (isMobile){
        mapTxtView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.startActivity(new Intent(MainActivity.this, EvacRouteTableActivity.class));
            }
        });
    } else {
        mapTxtView.setOnClickListener(new OnClickListener() {

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



}

采纳答案by yams

So I found what I needed with minimal change only to the activity_main.xml and here it is.

所以我只对activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView android:id="@+id/textView1_1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:singleLine="true" 
        android:text="To view Evacuation Routes - "
        android:textSize="20sp"
  android:textStyle="bold" 
        android:layout_marginRight="5dp"/>

    <TextView  android:id="@+id/txtView1"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="" 
        android:layout_toRightOf="@id/textView1_1"
  android:textSize="20sp"
  android:textStyle="bold"
  android:layout_alignParentRight="true"/>

</RelativeLayout>

So I added this code and it worked

所以我添加了这段代码并且它起作用了

回答by Anila

You can add a LinearLayout with orientation: "horizontal" and add both textviews in it.

您可以添加一个带有方向的 LinearLayout:“水平”并在其中添加两个文本视图。

<LinearLayout
         ...
         android:orientation="horizontal">
       <TextView .../>
       <TextView .../>
</LinearLayout>

回答by marshallino16

If you want to display two object side by side use android:layout_weight="2"property.

如果要并排显示两个对象,请使用ndroid:layout_weight="2"属性。