【问题标题】:not able to align two text views on the same line in android无法在android中的同一行上对齐两个文本视图
【发布时间】:2014-07-24 11:03:09
【问题描述】:

xml文件是,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"  
  android:id="@+id/profileLayout">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Super Powers"  
    />
</LinearLayout>

在 Activity 文件中,我试图添加一个文本字段,其值将动态变化(值将是 ON 或 OFF)并且该文本应与“Super Powers”内联

TextView valueTV = new TextView(this);
valueTV.setText("OFF");
valueTV.setId(5);
valueTV.setGravity(Gravity.RIGHT);
valueTV.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

((LinearLayout) linearLayout).addView(valueTV);

谁能告诉我如何使两个 textView 内联?

【问题讨论】:

  • 只需从您的布局中删除此 android:orientation="vertical" 行。

标签: android android-layout android-intent android-activity android-fragments


【解决方案1】:

您的 LinearLayout 具有垂直方向。它应该是水平的,以便能够将其子项放置在同一行中。

【讨论】:

  • thanku Seraphis...它现在在同一行。你知道如何使文本与行的右侧对齐吗?
  • 我可以看到两种方法:1. 将第二个TextView 设置为matchParentgravity(不是layout_gravity)到右侧。 2. 将LinearLayout 更改为RelativeLayout 并从代码中随意放置。
【解决方案2】:

尝试将布局更改为“相对布局”,然后使用 XML

【讨论】:

    【解决方案3】:

    请将您的方向从垂直更改为水平。现在您的文本视图将水平定向。

    【讨论】:

      【解决方案4】:

      使用此代码设计 xml

       <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal" >
      
          <TextView
              android:id="@+id/textView1"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:gravity="center"
              android:text="Super power"
              android:textAppearance="?android:attr/textAppearanceLarge" />
      
          <LinearLayout
              android:id="@+id/lin1"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:orientation="vertical" >
          </LinearLayout>
      
      </LinearLayout>
      

      并在 ID 为“lin1”的 LinearLayout 中添加动态文本视图 希望它对你有用

      【讨论】:

        【解决方案5】:

        试试这个方法:

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
        
          <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Super Powers : "  
            />
        
          <TextView
              android:id="@+id/txt_on_off"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="OFF" />
        
        </LinearLayout>
        
        </LinearLayout>
        

        并在您的 java 文件中引用 ON OFF textview ,如下所示:

        TextView txtOnOff;
        
        txtOnOff=(TextView)findViewById(R.id.txt_on_off);
        txtOnOff.setText("ON");
        

        就是这样

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-07-05
          • 1970-01-01
          • 1970-01-01
          • 2021-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多