在 parent 视图的两侧放置两个 wrap_content 宽度的多行 TextView
Place two multiline TextViews with wrap_content width on both sides of the parent view
请帮助我实现以下两个 TextView 的排列:
- 左边是一个带标题的简单TextView,右边是另一个TextView,左边有一个Drawable(这很重要,因为它我不能使用
match_parent
)。两个 TextView 都应 wrap_content
并按到它们的一侧。
- 如果其中一个 TextView 太长,它应该靠在另一个 TextView 上并包裹自己的文本。
- 如果两个 TextView 都很长,那么它们应该占用相同的空间 space。 None 应该被推到 parent 视图之外。
<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
可能会占用整个可用空间 space,因此您可以添加一些 maxWidth
参数 - 如果它不受 TextView
本身然后你可以将第一个包装到 LinearLayout
(它支持 maxWidth
舒尔)。另一种方法可能是使用 TableLayout
和 TableRow
视图或 ConstraintLayout
试试这个 -
<?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>
您可以同时使用 LinearLayout
或 ConstraintLayout
,这里主要是 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>
看看这个
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);
}
有点长。但这有效。
你试过Flexbox layout了吗?看起来它涵盖了你的情况。
请帮助我实现以下两个 TextView 的排列:
- 左边是一个带标题的简单TextView,右边是另一个TextView,左边有一个Drawable(这很重要,因为它我不能使用
match_parent
)。两个 TextView 都应wrap_content
并按到它们的一侧。 - 如果其中一个 TextView 太长,它应该靠在另一个 TextView 上并包裹自己的文本。
- 如果两个 TextView 都很长,那么它们应该占用相同的空间 space。 None 应该被推到 parent 视图之外。
<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
可能会占用整个可用空间 space,因此您可以添加一些 maxWidth
参数 - 如果它不受 TextView
本身然后你可以将第一个包装到 LinearLayout
(它支持 maxWidth
舒尔)。另一种方法可能是使用 TableLayout
和 TableRow
视图或 ConstraintLayout
试试这个 -
<?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>
您可以同时使用 LinearLayout
或 ConstraintLayout
,这里主要是 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>
看看这个
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);
}
有点长。但这有效。
你试过Flexbox layout了吗?看起来它涵盖了你的情况。