【问题标题】:Aligning textviews on the left and right edges in Android layout在Android布局中对齐左右边缘的文本视图
【发布时间】:2011-01-07 03:22:58
【问题描述】:

我开始使用 Android。我无法进行简单的布局。

我想使用LinearLayout 将两个TextViews 放置在一行中。一个 TextView 在左侧,另一个在右侧(类似于 CSS 中的 float:left、float:right)。

这可能吗,还是我需要使用不同的ViewGroup 或进一步的布局嵌套来完成它?

这是我目前所拥有的:

<?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:padding="10sp">
<TextView android:id="@+id/mytextview1" android:layout_height="wrap_content" android:text="somestringontheleftSomestring" android:layout_width="wrap_content"/>
<TextView android:id="@+id/mytextview2" android:layout_height="wrap_content" android:ellipsize="end"
    android:text="somestringontheright" android:layout_width="wrap_content"/>
</LinearLayout>

【问题讨论】:

    标签: android


    【解决方案1】:

    RelativeLayoutlayout_alignParentLeftlayout_alignParentRight 一起使用:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout01" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:padding="10dp">
    
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" 
            android:id="@+id/mytextview1"/>
    
        <TextView 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_alignParentRight="true" 
            android:id="@+id/mytextview2"/>
    
    </RelativeLayout>
    

    另外,you should probably be using dip (or dp) rather than sp in your layoutsp 反映文本设置以及屏幕密度,因此它们通常仅用于调整文本项的大小。

    【讨论】:

    • 为了防止内容重叠,可能还想在第一个文本视图中添加 'android:layout_toLeftOf="@+id/mytextview2"'
    • 对于动态表,你可以这样做: TableRow.LayoutParams col1Params = new TableRow.LayoutParams(); // 总结行的内容 col1Params.height = LayoutParams.WRAP_CONTENT; col1Params.width = LayoutParams.WRAP_CONTENT; // 设置重力使列的重力居中 col1Params.gravity = Gravity.LEFT;
    • 为了更好的兼容性和对不同设备屏幕的支持,请使用“android:layout_alignParentEnd="true"”(使用layout_alignParentRight时)和“android:layout_alignParentStart="true"”(使用layout_alignParentLeft时)。
    【解决方案2】:

    您可以使用重力属性来“浮动”视图。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <LinearLayout 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center_vertical|center_horizontal"
                android:orientation="horizontal">
    
            <TextView  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:gravity="left"
                android:layout_weight="1"
                android:text="Left Aligned"
                />
    
            <TextView  
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:gravity="right"
                android:layout_weight="1"
                android:text="Right Aligned"
                />
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 不幸的是,这似乎不会导致两个项目在同一行
    • 重力不只影响“文本对齐”吗?
    • 对不起,它的工作。我忘了添加 android:layout_weight="1"。添加后就可以了。
    【解决方案3】:

    可以使用LinearLayout 完成(比相对布局选项开销更少,控制更多)。为第二个视图留出剩余空间,以便gravity 可以工作。测试回 API 16。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Aligned left" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:text="Aligned right" />
    </LinearLayout> 
    

    如果要限制第一个文本视图的大小,请执行以下操作:

    根据需要调整权重。相对布局不允许您设置这样的百分比权重,只能设置其中一个视图的固定 dp

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Aligned left but too long and would squash the other view" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:gravity="end"
            android:text="Aligned right" />
    </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      即使有 Rollin_s 的提示,Dave Webb 的回答对我也不起作用。右侧 TextView 中的文本仍然与左侧 TextView 中的文本重叠。

      我最终得到了我想要的行为:

      <RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/RelativeLayout01" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent"
          android:padding="10dp">
      
      <TextView 
          android:id="@+id/mytextview1"
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true" />
      
      <TextView 
          android:id="@+id/mytextview2"
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_toRightOf="@id/mytextview1"
          android:gravity="right"/>
      
      </RelativeLayout>
      

      请注意,mytextview2 将 "android:layout_width" 设置为 "match_parent"

      希望这对某人有所帮助!

      【讨论】:

        【解决方案5】:

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hello world" />
        
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gud bye" />
        

        【讨论】:

          【解决方案6】:

          此代码会将控件分成两个相等的边。

              <LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/linearLayout">
              <TextView
                  android:text = "Left Side"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight = "1"
                  android:id = "@+id/txtLeft"/>
          
              <TextView android:text = "Right Side"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:layout_weight = "1"
                  android:id = "@+id/txtRight"/>
              </LinearLayout>
          

          【讨论】:

          • 非常简单且有用的答案。
          【解决方案7】:

          如果您希望左右元素包裹内容但有中间空间

          <LinearLayout
                  android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">
              <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"/>
              <Space
                      android:layout_width="0dp"
                      android:layout_height="match_parent"
                      android:layout_weight="1"/>
              <Button
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"/>
          </LinearLayout>
          

          【讨论】:

            【解决方案8】:

            还有很多其他方法可以做到这一点,我会做这样的事情。

            <RelativeLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            
                <TextView
                    android:id="@+id/textRight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_marginTop="30dp"
                    android:text="YOUR TEXT ON THE RIGHT"
                    android:textSize="16sp"
                    android:textStyle="italic" />
            
            
                <TextView
                    android:id="@+id/textLeft"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginTop="30dp"
                    android:text="YOUR TEXT ON THE LEFT"
                    android:textSize="16sp" />
            </RelativeLayout>
            

            【讨论】:

              【解决方案9】:

              戴夫韦伯的回答确实对我有用。谢谢! 这是我的代码,希望这对某人有帮助!

              <RelativeLayout
                  android:background="#FFFFFF"
                  android:layout_width="match_parent"
                  android:minHeight="30dp"
                  android:layout_height="wrap_content">
                  <TextView
                      android:height="25dp"
                      android:layout_width="wrap_content"
                      android:layout_marginLeft="20dp"
                      android:text="ABA Type"
                      android:padding="3dip"
                      android:layout_gravity="center_vertical"
                      android:gravity="left|center_vertical"
                      android:layout_height="match_parent" />
                  <TextView
                      android:background="@color/blue"
                      android:minWidth="30px"
                      android:minHeight="30px"
                      android:layout_column="1"
                      android:id="@+id/txtABAType"
                      android:singleLine="false"
                      android:layout_gravity="center_vertical"
                      android:layout_weight="1"
                      android:layout_height="wrap_content"
                      android:layout_alignParentRight="true"
                      android:layout_marginRight="20dp"
                      android:layout_width="wrap_content" />
              </RelativeLayout>
              

              图片: Image

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2022-01-19
                • 2014-10-24
                • 1970-01-01
                • 1970-01-01
                • 2012-05-08
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多