【问题标题】:Place two multiline TextViews with wrap_content width on both sides of the parent view在父视图的两侧放置两个宽度为 wrap_content 的多行 TextView
【发布时间】:2021-02-21 01:53:50
【问题描述】:

请帮我实现两个TextViews的如下排列:

  1. 左边是一个简单的带有标题的TextView,右边是另一个TextView,左边有一个Drawable(它很重要,因为它我不能使用match_parent)。两个 TextView 都应该是 wrap_content 并压到它们的一边。
  2. 如果其中一个 TextView 太长,它应该靠在另一个 TextView 上并换行自己的文本。
  3. 如果两个 TextView 都很长,那么它们应该占用相同的空间。它们都不应该被推到父视图之外。

【问题讨论】:

  • 使用带权重的线性布局来实现这一点。
  • @ShailendraMadda 不幸的是,我不能使用权重,因为这样正确的 TextView 将始终占据 50% 的空间,尽管它的文本很长

标签: android layout textview android-constraintlayout


【解决方案1】:
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:gravity="left" />

    <TextView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/left"
        android:gravity="right" />

</RelativeLayout>

注意 left TextView 如果文本很长,可能会占用整个可用空间,因此您可以添加一些 maxWidth 参数 - 如果 TextView 本身不支持它,那么您可以将第一个参数包装到 @987654326 @(支持 maxWidth 用于舒尔)。另一种方法可能是使用TableLayoutTableRow 视图或ConstraintLayout

【讨论】:

  • 感谢您的回答。感谢您的努力。
  • 如果有帮助,请考虑支持/接受答案 :) 祝你好运!
【解决方案2】:

试试这个 -

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

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/text2"
    android:text="TextView1" />

<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_alignParentRight="true"
    android:drawableLeft="@drawable/basket"
    android:text="TextView2" />
</RelativeLayout>

【讨论】:

    【解决方案3】:

    LinearLayoutConstraintLayout 都可以使用,这里主要是android:maxWidth,我们需要给出一个视图,另一个视图会根据它进行调整。

    ConstraintLayout 示例

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/start"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toStartOf="@+id/end"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:text="Loooooooooong Texxxxxxxt!!!!!!!!!!!!" />
    
        <TextView
            android:id="@+id/end"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="250dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:text="May be this is short"/>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    LinearLayout 示例:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Loooooooooong Texxxxxxxt!!!!!!!!!!!!!!!!!!!!!!" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="250dp"
            android:text="May be this is short with maxwidth"/>
    
    </LinearLayout>
    

    【讨论】:

    • 这是一个可靠的解决方案,我喜欢它。如果没有更灵活的选项,我会检查它作为正确答案。
    【解决方案4】:

    看看这个

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/constraintLayout"
        tools:context=".MainActivity"
        tools:ignore="HardcodedText">
    
        <TextView
            android:id="@+id/startTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="On"
            android:gravity="center"
            android:textSize="32sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/endTextView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/endTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_margin="8dp"
            android:text="If one of the TextViews is too long"
            android:textSize="20sp"
            app:drawableStartCompat="@android:drawable/ic_btn_speak_now"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/startTextView"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    MainActivity.java

     private TextView startTV, endTV;
        private ConstraintLayout constraintLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            constraintLayout = findViewById(R.id.constraintLayout);
            startTV = findViewById(R.id.startTextView);
            endTV = findViewById(R.id.endTextView);
    
    
            constraintLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    int systemWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
    
                    int startTVWidth = startTV.getMeasuredWidth();
                    int endTVWidth = endTV.getMeasuredWidth();
    
                    if ((startTVWidth + endTVWidth) >= systemWidth) {
    
                        if (startTVWidth > endTVWidth) {
    
                            startTV.setMaxWidth(systemWidth - endTVWidth - 24);
    
                            startTV.setPadding(12, 12, 12, 12);
                            endTV.setPadding(12, 12, 12, 12);
    
                        } else if (endTVWidth > startTVWidth) {
    
                            endTV.setMaxWidth(systemWidth - startTVWidth - 24);
    
                            startTV.setPadding(12, 12, 12, 12);
                            endTV.setPadding(12, 12, 12, 12);
                        }
                    }
                }
            }, 1);
        }
    

    有点长。但这行得通。

    【讨论】:

    • 感谢您的回答!我真的很感谢你的努力。但我认为这个解决方案对于这样的任务来说太复杂了。
    • @Nikolay 这是我能找到的唯一解决方案,既可以在TextViews 上使用wrap_content,也可以让大TextView 靠在另一个TextView 上。我找不到任何不太复杂的解决方案......谢谢。快乐编码:)
    【解决方案5】:

    你试过Flexbox layout吗?看起来它涵盖了您的情况。

    【讨论】:

    • 谢谢!这似乎是最正确的方法。我以前从未尝试过使用 FlexLayout。
    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多