【问题标题】:LinearLayout horizontal not working correctlyLinearLayout 水平无法正常工作
【发布时间】:2019-02-12 11:55:20
【问题描述】:

我尝试在我的 TextView 中添加自定义引号。我使用水平创建 LinearLayout 并添加开始/结束自定义文本视图。这是我的来源

            <LinearLayout
            android:id="@+id/description_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/dimen_p_30"
            android:layout_marginTop="@dimen/dimen_p_25"
            android:layout_marginRight="@dimen/dimen_p_30"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/helvetica_regular"
                android:gravity="top"
                android:text="“"
                android:textColor="#E8E9EF"
                android:textSize="@dimen/dimen_p_46" />
            <TextView
                android:id="@+id/descriptionTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/myriad_geo_medium"
                android:gravity="top"
                android:maxLines="150"
                android:text="test message"
                android:textColor="@color/gray"
                android:textSize="@dimen/dimen_p_14" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:fontFamily="@font/helvetica_regular"
                android:gravity="bottom"
                android:text="”"
                android:textColor="#E8E9EF"
                android:textSize="@dimen/dimen_p_46" />

        </LinearLayout>

一切都很完美,但是当我的descriptionTextView 有大文本并且需要两行时,第三个文本视图没有显示。有什么方法可以解决这个问题还是我的方法是正确的? 谢谢

【问题讨论】:

标签: android android-linearlayout textview


【解决方案1】:

仅将带有感叹号的完整文本添加到一个 TextView。使用 Spannable 字符串将颜色设置为感叹号。

TextView textView = (TextView)findViewById(R.id.mytextview01);    
String mText = textView.getText();
Spannable wordtoSpan = new SpannableString(mText);          
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), (mText.Length()-2), (mText.Length()-1), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
textView.setText(wordtoSpan);

【讨论】:

  • 是的,你是对的,但最后的引用没有显示 textview 的结尾
  • 您是否希望您的感叹与您的文字颜色不同?
  • 我只是想用不同的颜色和不同的文本大小添加感叹号结束/开始位置
【解决方案2】:

像这样将weight 用于您的TextViews

   <LinearLayout
    android:id="@+id/description_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="@dimen/dimen_p_30"
    android:layout_marginTop="@dimen/dimen_p_25"
    android:layout_marginRight="@dimen/dimen_p_30"
    android:orientation="horizontal"
    android:weightSum="3"
    >

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="@font/helvetica_regular"
        android:gravity="top"
        android:text="“"
        android:textColor="#E8E9EF"
        android:textSize="@dimen/dimen_p_46" />
    <TextView
        android:id="@+id/descriptionTextView"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="@font/myriad_geo_medium"
        android:gravity="top"
        android:maxLines="150"
        android:text="test message"
        android:textColor="@color/gray"
        android:textSize="@dimen/dimen_p_14" />

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="@font/helvetica_regular"
        android:gravity="bottom"
        android:text="”"
        android:textColor="#E8E9EF"
        android:textSize="@dimen/dimen_p_46" />

</LinearLayout>

【讨论】: