繁体   English   中英

Android两个Textview在一行中带有“ wrapcontent”

[英]Android two Textview with “wrapcontent” in single line

我无法获得看似简单的布局配置!!

我在相对(水平方向尝试线性)中有两个textview都“ wrapcontent”-我希望它们彼此相邻。 就像是:

  1. 第一个文字|| 第二文字[ 这是较小的文字大小 ]

    (对于“小标题”效果很好)

在此处输入图片说明

当第一个文本很长-超过一行中可以显示的长度时,第一个文本将转到第二行,第二个Textview消失(向右深处)。 如下所示:

2.对于长标题-观察计数向右消失

在此处输入图片说明

          <RelativeLayout
                android:orientation="horizontal"
                android:id="@+id/feedHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical">

                <TextView
                    android:id="@+id/textView1"
                    style="@style/BoldTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="0dp"
                    android:layout_alignParentLeft="true"
                    android:layout_marginTop="5dp"
                    android:paddingBottom="0dp"
                    android:text="Title Loooooooggggggg Title Looong"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/pageCounts"
                    style="@style/WayBoardCount"
                    android:layout_width="wrap_content"
                    android:minWidth="50dp"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@id/textView1"
                    android:layout_toRightOf="@id/textView1"
                    android:checked="false"
                    android:gravity="left|center_vertical"
                    android:text="3/5"
                    android:textAppearance="?android:attr/textAppearanceSmall" />
            </RelativeLayout>

我希望小文字(数量)在“ Looong”旁边,并且不要在右侧消失

  1. 尝试了“第二个文本视图的最小大小”
  2. 水平方向的线性布局

编辑:在这个线程中给出的一些解决方案/答案仍然不能解决我的问题(为了他人的利益,我在下面提供了我的输出):

亚历山大: 在此处输入图片说明

您可以尝试

    <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal"
   android:weightSum="1" >
<TextView
     android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center"
    android:text="Title Loooooooggggggg Title Looong"
    android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
    android:id="@+id/pageCounts"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="center"
    android:text="3/5"
    android:textAppearance="?android:attr/textAppearanceSmall"" />
 </LinearLayout>

注意

如果Text太大,则无法单行显示 那时您可以使用android:ellipsize

android:ellipsize="end"

我了解您的问题。 因此,您可以使用spannable 有关指标的有用链接。 希望它可以帮助教程

简单的鳕鱼:

private void updateText(TextView textView, String bigText, String smallText) {
    SpannableString spannableString = new SpannableString(bigText + smallText);
    spannableString.setSpan(new RelativeSizeSpan(1.4f), 0, bigText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new RelativeSizeSpan(1.0f), bigText.length(), spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString);
}

就这样吧

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <TextView
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:text="VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongText"
      android:layout_weight="1"/>

  <TextView
      android:layout_width="match_parent"
      android:gravity="start|bottom"
      android:layout_height="5dp"
      android:textSize="5sp"
      android:textAppearance="?android:attr/textAppearanceSmall"
      android:text="SmallText"
      android:layout_weight="1"/>

</LinearLayout>

您也可以使用RelativeLayout进行相同操作,以下是一个伪代码

         <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/feedHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <TextView
                 android:id="@+id/pageCounts"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentRight="true"
                 android:layout_centerInParent="true"
                 android:checked="false"
                 android:text="3/5"
                 android:textAppearance="?android:attr/textAppearanceSmall" />

            <TextView
                 android:id="@+id/textView1"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentLeft="true"
                 android:layout_marginBottom="0dp"
                 android:layout_marginTop="5dp"
                 android:layout_toLeftOf="@+id/textView2"
                 android:paddingBottom="0dp"
                 android:text="Title Loooooooggggggg Title Looong Loong"
                 android:textAppearance="?android:attr/textAppearanceMedium" />

         </RelativeLayout>

显然我在寻找“ Span-able String”!

此处的详细信息: 同一TextView中字符串的不同字体大小

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM