【问题标题】:how to define correctly textview width如何正确定义文本视图宽度
【发布时间】:2015-12-18 22:55:12
【问题描述】:

大家好,我的 textview 的宽度有点问题 看起来像this

如你所见,我的文本视图比我的文本大:/

这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="destinataire"
        android:id="@+id/tv_destinataire"
        android:layout_gravity="left|top"
        android:layout_weight="1"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:textColor="#000000" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="envoyeur"
        android:id="@+id/tv_envoyeur"
        android:layout_gravity="right|top"
        android:layout_weight="1"
        android:layout_marginRight="25dp"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        android:textColor="#ffffff"
        android:layout_marginBottom="10dp" />
</LinearLayout>

我想要this 之类的东西,如果消息包含一些单词“wrap_content”,而消息很长,例如最大宽度的 70%:

谢谢你:)

【问题讨论】:

    标签: java android-studio textview sms


    【解决方案1】:

    您可以直接在活动中添加一些逻辑,而不是在 XML 文件中。您可以通过 Java 代码设置宽度,例如:

    TextView t = (TextView)findViewById(R.id.myTextView);
    

    其中 myTextView 是您在 XML 中声明的 id。

    现在继续写一些逻辑...... 如有必要:

    t.setWidth(200);
    

    注意200只是一个例子,你可以在之前计算出你需要的宽度。

    【讨论】:

      【解决方案2】:

      如果我是你,我可能会使用相对布局并向包含文本的 TextView 添加 maxWidth 限制,并且随着对话框的进行,只需将 TextView 放在最后一个 TextView 下方并使用 alignStart/alightParentStart 和 alignEnd/alignParentEnd 来指示谁在说话.

      当有新消息到达时

      1. 准备您的相关布局参数,添加布局规则
      2. 为您的文本设置文本、最大宽度限制、布局参数和其他样式
      3. 将视图添加到可滚动的相对布局容器中

      如果您有任何阅读问题,请原谅我的英语不好。 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多