【问题标题】:how to Combine TextViews in xml?如何在 xml 中合并 TextViews?
【发布时间】:2018-05-24 03:16:38
【问题描述】:

我正在尝试在 android 的 xml 文件中构建此视图,以在 recyclerview 中表达一个列表项

但我得到了这个视图

这是我的代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="horizontal"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image_news"
            android:layout_width="100dp"
            android:layout_height="100dp"
            app:srcCompat="@drawable/ic_perm_identity_grey_400_48dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:orientation="horizontal"
        android:padding="10dp">

        <TextView
            android:id="@+id/txt_news_name"
            android:layout_width="135dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:text="Mohamed Alaa"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/txt_news_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="added a new photo to the album"
            android:textSize="16sp" />

    </LinearLayout>

</LinearLayout>

如何改进我的代码以获得所需的结果?

我希望任何答案都只应用于 xml 文件而不编写 java 代码

【问题讨论】:

  • 你为什么把 ImageView 单独放在一个布局中?这真的是一种反模式。尝试使用RelativeLayout,将所有视图组合在一个独特的容器中。

标签: android xml


【解决方案1】:

做这样的事情:

TextView tv = (TextView) findViewById(R.id.yourTextView);

String name = "Mohammed Alaa";
String album = "Instagram Photos";

SpannableStringBuilder sb = new SpannableStringBuilder();

sb.append(name,new StyleSpan(android.graphics.Typeface.BOLD),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.append(" added a new photo to the album ");
sb.append(album,new StyleSpan(android.graphics.Typeface.BOLD),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
sb.append(".");

tv.setText(sb);

【讨论】:

  • 我怎样才能将上面的 list_item 布局文件加入到 java 文件中??
  • 使用 LayoutInflater
  • View v = LayoutInflater.from(context).inflate(R.layout.yourLayout,null); 这个View v 可以是一个LinearLayout。
猜你喜欢
  • 1970-01-01
  • 2011-08-06
  • 1970-01-01
  • 1970-01-01
  • 2013-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
相关资源
最近更新 更多