You*_*sef 27 android textview android-relativelayout
我需要实现以下布局:
我在相对布局中有两个TextView:绿色的是固定文本wrap_content
,黑色有动态文本wrap_content
.黑色文本可以更改并变得非常长.我希望黑色TextView随文本一起展开,直到绿色视图到达父级的末尾.如果发生这种情况,黑色TextView应该停止扩展并椭圆化结束.
我怎样才能做到这一点?
我尝试了什么:
<RelativeLayout
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/leftTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_alignParentLeft="true"
android:textSize="18sp" />
<TextView
android:id="@+id/rightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/leftTextView"
android:textSize="18sp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
但是当黑色文本越来越大时,它会将绿色文本推出视图
nsh*_*ura 22
您可以通过存档该布局TableLayout
和shrinkColumns
属性.
<?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">
<!-- Row 1 -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="end"
android:text="abcdefghijklmnopqrstuvwxyz"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="none"
android:text="rightText"/>
</TableRow>
</TableLayout>
<!-- Row 2 -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="end"
android:text="Longer Text view abcdefghijklmnopqrstuvwxyz"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="none"
android:text="rightText"/>
</TableRow>
</TableLayout>
<!-- Row 3 -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="end"
android:text="Text view that is very logn and will not fit the parent width abcdefghijklmnopqrstuvwxyz"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:maxLines="1"
android:ellipsize="none"
android:text="rightText"/>
</TableRow>
</TableLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是同样的问题;)
小智 11
这是一个 ConstraintLayout 解决方案。
魔术是链。
<?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="wrap_content"
android:padding="20dp">
<TextView
android:id="@+id/leftTv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#333333"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/rightTv"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap"
tools:text="Text view that is very long and will not fit the" />
<TextView
android:id="@+id/rightTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:textColor="#21b38a"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/leftTv"
app:layout_constraintTop_toTopOf="parent"
tools:text="Text View" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
我建议将两个包装TextView
成LinearLayout
. 然后适用android:weightSum="1"
于此LinearLayout
,适用android:layout_weight="1"
于TextView
必须扩展的孩子。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:padding="8dp"
android:text="blabla bla bla bla bla bla bla bla bla bla bla bla bla "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="static text"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
请记住设置这两个属性以使其完美运行:
android:ellipsize="end"
android:maxLines="1"
Run Code Online (Sandbox Code Playgroud)
并且LinearLayout
必须有android:layout_width="wrap_content"
如果您希望它ViewGroup
占据整个空间,请用另一个包装它ViewGroup
:
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<!--The previous view group-->
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
可以借助layout_weight
附带的 attirbute创建您期望的布局LinearLayout
。因此,您可能需要使用LinearLayout
withweightSum
和layout_weight
(传递给其childview
)属性。
这是一个布局,它将布局的宽度分为四个部分,两个部分之间的比例为3:1Textview
。完整的 rightTextView
使 leftTextView
显示尽可能多的文本,而不会干扰 right TextView
。
请随意根据您的需要尝试变体weightSum
和组合。layout_weight
布局:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="StackOverflow StackOverflow StackOverflow "
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="3"
android:maxLines="1"
android:text="TextView"
android:textColor="@android:color/white" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)