【问题标题】:How to display 2 textviews in the same line in Android如何在 Android 的同一行中显示 2 个文本视图
【发布时间】:2013-04-17 01:44:39
【问题描述】:

我有我的布局 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
     android:id="@+id/titlename"  
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:text="@string/HostName"
 android:layout_weight="0"

/>
<TextView
     android:id="@+id/name"  
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
 android:layout_weight="0"
/>
</LinearLayout>

当我执行上述操作时,我的输出如下:

但我的要求是让我的输出如下:

|   text1:    text2    |

有人可以帮忙吗?

【问题讨论】:

  • 您是否尝试过 android:layout_weight="0.5" 与两个文本视图?这会给两者的一半宽度?

标签: android android-layout android-intent android-widget


【解决方案1】:

使用以下代码

更新

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal"
   android:weightSum="1" >
<TextView
    android:id="@+id/titlename"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center"
    android:text="hi" />
<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center"
    android:text="2hi2" />
 </LinearLayout>

【讨论】:

    【解决方案2】:

    将 android:layout_width 设为 0dp。然后为每个文本视图设置 0.5 个权重。每个文本视图将填充父布局的一半宽度。比如:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
         android:id="@+id/titlename"  
    android:layout_width="0dp"
    android:layout_height="wrap_content" 
    android:text="@string/HostName"
     android:layout_weight="0.5"
    
    />
    <TextView
         android:id="@+id/name"  
    android:layout_width="0dp"
    android:layout_height="wrap_content" 
     android:layout_weight="0.5"
    />
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      为您的 LinearLayout 设置 android:orientation="horizontal"

      【讨论】:

        【解决方案4】:

        如下所示更新您的布局,除非您在父 LinearLayout 上设置 weightSum,否则您不希望包含 weight=0 属性。加上 weights 和 weightSum 并不真正符合您的要求。

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
            <TextView
                 android:id="@+id/titlename"  
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" 
                 android:text="@string/HostName"
        
                />
            <TextView
                 android:id="@+id/name"  
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content" 
                />
        </LinearLayout>
        

        【讨论】:

          【解决方案5】:

          实际上,您的原始代码在我的 ADT 中完美运行。您是否更改了更新第二个文本视图的 Java 代码中的文本大小或行填充?

          【讨论】:

            【解决方案6】:

            我会去掉weight 属性。这是我能看到的唯一一件事,那就是搞砸了。另请注意,fill_parent 已弃用,您应该改用match_parent

            或者,如果您出于某种原因需要weight,那么如果您希望它们各自占据屏幕的一半,请将它们更改为 1。

            另外,如果使用weight,那么你应该设置android:layout_width="0dp"

            【讨论】:

              【解决方案7】:
              <?xml version="1.0" encoding="utf-8"?>
              <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
              
              <TextView
                   android:id="@+id/titlename"  
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" 
                   android:text="@string/HostName"
              />
              <TextView
                   android:id="@+id/name"  
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" 
              />
              </LinearLayout>
              

              【讨论】:

                【解决方案8】:

                你可以尝试使用RelativeLayout

                这样您就可以在 titlename 元素上设置 alignParentTop="true" 并在 name 元素上设置名称 alignTo="@id/titlename"toRightOf="@id/titlename"

                【讨论】:

                  【解决方案9】:

                  创建一个 LinearLayout 包含 2 个 TextViews

                  <?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:orientation="vertical"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      tools:context=".MainActivity">
                  
                      <Button
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:id="@+id/btnLogin"
                          android:layout_below="@id/etPassword"
                          android:layout_marginTop="20dp"
                          android:layout_marginLeft="50dp"
                          android:layout_marginRight="50dp"
                          android:text="Login"/>
                  
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_below="@id/btnLogin"
                          android:layout_marginTop="10dp"
                          android:gravity="center">
                  
                          <TextView
                              android:id="@+id/tvIAlwaysHaveAccount"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_below="@id/btnLogin"
                              android:text="I always have an account?" />
                  
                          <TextView
                              android:id="@+id/tvForgotPassword"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_toRightOf="@id/tvIAlwaysHaveAccount"
                              android:layout_marginLeft="15dp"
                              android:text="Forgot password?" />
                  
                      </LinearLayout>
                  
                  
                  </RelativeLayout>
                  

                  和你的节目一样:"

                  【讨论】:

                    猜你喜欢
                    • 2023-04-03
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-01-15
                    • 1970-01-01
                    • 2014-04-23
                    • 2020-02-22
                    • 1970-01-01
                    相关资源
                    最近更新 更多