【问题标题】:Expand one textview space with respect to another相对于另一个扩展一个文本视图空间
【发布时间】:2017-12-19 06:30:39
【问题描述】:
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textSize="24sp"
            android:text="Name."
            android:textColor="#000"
            android:textStyle="bold"
            android:background="#c91799c9"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textSize="17sp"
            android:paddingLeft="8dp"
            android:text="Paragraph"
            android:textColor="#000"
            android:textStyle="bold"
            android:background="#3f1799c9"
            android:layout_height="wrap_content" />

    </LinearLayout>

在给定的代码中,我在水平视图中有 2 个文本视图。 我希望当一个textview的文本进入另一行时,另一个textview空间也应该根据第一个增加

在给定的图像段落中可以是 3-4 行,但我希望 Name textview 应该垂直展开

【问题讨论】:

标签: android textview


【解决方案1】:

试试这个

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textSize="24sp"
            android:text="Name."
            android:textColor="#000"
            android:textStyle="bold"
            android:background="#c91799c9"
            android:layout_height="match_parent" />
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:textSize="17sp"
            android:paddingLeft="8dp"
            android:text="Paragraph"
            android:textColor="#000"
            android:textStyle="bold"
            android:background="#3f1799c9"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>

输出

【讨论】:

    【解决方案2】:

    试试这个, 在 textView 中设置 android:layout_height="match_parent" 也可以在 textView android:textSize="24sp" 中设置 Textsize 相同

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2">
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#c91799c9"
        android:text="Name."
        android:textColor="#000"
        android:textSize="24sp"
        android:textStyle="bold" />
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#3f1799c9"
        android:paddingLeft="8dp"
        android:text="Paragraph"
        android:textColor="#000"
        android:textSize="17sp"
        android:textStyle="bold" />
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      试试这个:

      只需给 ma​​tch_parent

      提供高度

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:weightSum="2">
      
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="#c91799c9"
              android:text="Name."
              android:textColor="#000"
              android:textSize="24sp"
              android:textStyle="bold" />
      
          <TextView
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:background="#3f1799c9"
              android:paddingLeft="8dp"
              android:text="paragraph"
              android:textColor="#000"
              android:textSize="17sp"
              android:textStyle="bold" />
      
      </LinearLayout>
      

      如果您希望文本居中,您可以在两个文本视图中添加android:gravity="center_vertical"

      【讨论】:

      • 如果这解决了你的问题,那么请给答案​​投票:)
      【解决方案4】:

      将重心赋予第一个 TextView.ie 将以下代码添加到 First TextView 'Name'。

      android:layout_gravity="center"
      

      并将两个 TextView 的高度更改为 match_parent。

       android:layout_height="match_parent"
      

      【讨论】:

        【解决方案5】:

        你必须在布局显示后设置宽度,否则你会得到一个 错误,有一个命令 .onPost 可以用来避免这种情况 错误。

        <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
                    <ImageView
                    android:id="@+id/my_image"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"/>
                    <EditText
                    android:id="@+id/et_user"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"/>
                    <Button
                    android:id="@+id/bt_start"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"/>
        </LinearLayout>
        

        java 代码必须放在 OnCreate 方法中,但在之后 设置内容视图

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView my_image = findViewById(R.id.my_image);
        Button bt_start = findViewById(R.id.bt_start);
        EditText et_user = = findViewById(R.id.et_user);
        if ((my_image != null))
            {
                my_image.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        bt_start.setWidth(my_image.getWidth());
                        et_user.setWidth(my_image.getWidth());                                 
                    }
            });
        }
        

        在我的示例中,您使用my_image.getWidth() 获得图像视图宽度 并将此值设置为其他组件,例如 bt_start 或 et_user 但是 您可以在任何其他组件中执行此操作。

        my_image != null
        

        可以更改任何其他布尔比较,例如:

        bt_start != null
        et_user != null
        

        这只是为了确认您从中获得宽度的项目不为空,所以 你可以正确地得到宽度,如果这个项目是空的,你会得到一个 空点异常。

        【讨论】:

          【解决方案6】:
          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:weightSum="2"
              xmlns:android="http://schemas.android.com/apk/res/android">
          
             <TextView
                 android:layout_width="0dp"
                 android:layout_weight="1"
                 android:textSize="24sp"
                 android:text="Name"
                 android:textColor="#000"
                 android:textStyle="bold"
                 android:gravity="center"
                 android:background="#c91799c9"
                 android:layout_height="match_parent" />
             <TextView
                 android:layout_width="0dp"
                 android:layout_weight="1"
                 android:textSize="17sp"
                 android:paddingLeft="8dp"
                 android:text="Paragraph"
                 android:gravity="center"
                 android:textColor="#000"
                 android:textStyle="bold"
                 android:background="#3f1799c9"
                 android:layout_height="match_parent" />
          
          </LinearLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-05-01
            • 1970-01-01
            • 2022-07-03
            • 1970-01-01
            • 2016-12-31
            相关资源
            最近更新 更多