[英]Separate second line of TextView into another TextView with Java
[英]Add another textview under the previous textview as a second textview
伙计们,我试图添加另一个textview,但是当我尝试将新的textview放在先前的textview上时,如何使第二个textview下降为新的第二个textview
这是我的代码,希望您能对我有所帮助
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="@string/state1"
android:textSize="16sp" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@id/textView1"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/sayer1"
android:textSize="12sp" />
<TextView
android:id="@+id/thirdLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@id/textView2"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/sayer2"
android:textSize="12sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:text="@string/state2"
android:textSize="16sp" />
</RelativeLayout>
很简单,只需用LinearLayout包装这两个TextView并稍微对齐一点即可。
这将非常整洁,而不仅仅是定位在相对布局内部。 因为如果您想移动整个包装,TextView将一起移动。 因此您无需担心对齐每个TextView
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/state1"
android:textSize="16sp" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="@id/textView1"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/sayer1"
android:textSize="12sp" />
</LinearLayout>
您应该仅基于父级对齐一个TextView,其余基于该父级对齐。
换句话说,如果您未设置layout_参数,则应将其与父对象对齐,以便视图知道将其自身放置在何处。 现在,其他视图可以使用layout_参数针对有alignParent参数的观点。
如果将它们全部与父级对齐,则它们只会对齐以记住父级,而不会为其他TextView烦恼。
我想像一下,他们认为与父母保持一致是一种“锚”,而其他人则将自己置于反对“锚”的位置。
请尝试以下操作(出于可读性考虑,我将其删除为其他参数不是因为您应该删除它们!)
<TextView
android:id="@+id/textView1"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/secondLine"
android:layout_below="@id/textView1" />
//If You set it below, there's no reason to align it with the Parent
// you can now use margins instead
<TextView
android:id="@+id/thirdLine"
android:layout_below="@id/textView2" />
//Same scenario
<TextView
android:id="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
//In this case you DO need to align with parent because you are not setting a
//layout_ parameter
我希望这有助于您朝正确的方向发展。 另外,请记住,如果使用alignParent参数更改视图上的值,则其他具有layout_参数并且位置也会发生变化的视图。
将您的xml文件更改为以下内容。 可能对你有帮助
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/state1"
android:textSize="16sp" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/sayer1"
android:textSize="12sp" />
<TextView
android:id="@+id/thirdLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:ellipsize="marquee"
android:singleLine="true"
android:text="@string/sayer2"
android:textSize="12sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="@string/state2"
android:textSize="16sp" />
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.