Java 在Android应用程序中将注释文本包装在用户名下
我的问题是,当我在提要中将用户注释插入用户名右侧时,整个注释块都会显示在右侧。它看起来又粗又笨拙。我想实现一种类似Instagram的效果,其中第一行直接出现在用户名之后,第二行和所有后续行都在用户名下面。我想知道是否有什么常见的XML技巧可以像Instagram一样将我的注释字符串的第二行和所有后续行包装在用户名字符串下面 我的第二个想法是以编程方式分解comment对象中的行,并将几个短字符串附加在一起,形成一个单一的、内聚的注释块 有没有经验丰富的UI开发人员知道一些技巧?如何计算用户名的长度?我想只要缩进第一行,不管怎么做,只有假设每个人都有相同的用户名长度,才会起作用,这显然不是一个好的假设。如果没有,是否有人使用后端实现了这样的效果?如果是这样的话,那么开始这样的工作的第一个好方向是什么 XML:Java 在Android应用程序中将注释文本包装在用户名下,java,android,user-interface,parse-platform,Java,Android,User Interface,Parse Platform,我的问题是,当我在提要中将用户注释插入用户名右侧时,整个注释块都会显示在右侧。它看起来又粗又笨拙。我想实现一种类似Instagram的效果,其中第一行直接出现在用户名之后,第二行和所有后续行都在用户名下面。我想知道是否有什么常见的XML技巧可以像Instagram一样将我的注释字符串的第二行和所有后续行包装在用户名字符串下面 我的第二个想法是以编程方式分解comment对象中的行,并将几个短字符串附加在一起,形成一个单一的、内聚的注释块 有没有经验丰富的UI开发人员知道一些技巧?如何计算用户名的
<LinearLayout
android:id="@+id/comment1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp">
<RelativeLayout
android:id="@+id/comment1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/commentUser1"
tools:text="commentUsername"
android:textColor="#44d7a8"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/commentText1"
tools:text="This is a comment."
android:textColor="#000000"
android:paddingRight="10dp" />
</RelativeLayout>
</Linear Layout>
public void setCommentText1(String text) {
commentText1.setText(String.format("%s %s", new Object[]{commentUser1.getText().toString(), text}));
}
所以我做了一些研究,并使用SpannableString将LeadingMarginSpan设置为“dp”中的长度,该长度略大于用户名的长度!伟大的它起作用了。嗯,这是一个好的开始。它适用于一个唯一的用户名长度
现在,我必须根据用户名字符串的长度动态设置LeadingMarginSpan的长度
public void setCommentText1(String text) {
SpannableString s = new SpannableString(text);
s.setSpan(new android.text.style.LeadingMarginSpan.Standard(150, 0), 0, s.length(), 0);
commentText1.setText(s);
}
不管怎样,我希望这能帮助别人
更新1:
<LinearLayout
android:id="@+id/comment1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp">
<RelativeLayout
android:id="@+id/comment1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/commentUser1"
tools:text="commentUsername"
android:textColor="#44d7a8"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/commentText1"
tools:text="This is a comment."
android:textColor="#000000"
android:paddingRight="10dp" />
</RelativeLayout>
</Linear Layout>
public void setCommentText1(String text) {
commentText1.setText(String.format("%s %s", new Object[]{commentUser1.getText().toString(), text}));
}
我存储了用户名的字符串,所以我只得到了它的长度,然后将该值乘以一个我称为“usernameSpan”的因子(上面设置为“150”)
这仍然不是最好的答案。理想情况下,我会得到字符串的最大Ems值,并将其乘以用户名,这样LeadingMarginSpan就等于用户名+一点空间以允许注释的开头。我尝试了username.getMaxEms(),但似乎不起作用,因为username字符串是动态设置的,所以我不知道它是否真的知道填充字符串后整个字符串的最大Ems。如果有人有任何建议,我将不胜感激!谢谢
为什么不连接?
它们不应该只是连接在一起的一个原因是username TextView有一些编程元素,比如链接到概要文件。因此,最佳解决方案必须看起来像上面的某个东西
更新2:
<LinearLayout
android:id="@+id/comment1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp">
<RelativeLayout
android:id="@+id/comment1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/commentUser1"
tools:text="commentUsername"
android:textColor="#44d7a8"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/commentText1"
tools:text="This is a comment."
android:textColor="#000000"
android:paddingRight="10dp" />
</RelativeLayout>
</Linear Layout>
public void setCommentText1(String text) {
commentText1.setText(String.format("%s %s", new Object[]{commentUser1.getText().toString(), text}));
}
我已经删除了leadingMarginSpan(发现文本视图的宽度似乎非常不可靠),并选择将用户名和注释字符串直接放在彼此的顶部。comments字符串包含从我们的服务器中提取的用户名,以便间距与每个唯一的注释相匹配。如果我能以某种方式使这个组合字符串的用户部分不可见,那么我们就有了一个可行的解决方案,尽管有点混乱
最终工作方案:
<LinearLayout
android:id="@+id/comment1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp">
<RelativeLayout
android:id="@+id/comment1Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/commentUser1"
tools:text="commentUsername"
android:textColor="#44d7a8"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/commentText1"
tools:text="This is a comment."
android:textColor="#000000"
android:paddingRight="10dp" />
</RelativeLayout>
</Linear Layout>
public void setCommentText1(String text) {
commentText1.setText(String.format("%s %s", new Object[]{commentUser1.getText().toString(), text}));
}
对于我的特殊问题,我需要使用SpannableStringBuilder对concat字符串的第一部分(最终为CommentUsername1span)进行样式设置,以匹配原始用户名TextView的颜色,然后将第二部分(文本或注释字符串)保留为无格式。它起作用了
public void setCommentUser1(String text) {
commentUser1.setText(text);
}
public void setCommentText1(String text) {
SpannableStringBuilder builder = new SpannableStringBuilder();
String commentUsername1 = commentUser1.getText().toString();
SpannableString commentUsername1Spannable = new SpannableString(commentUsername1);
commentUsername1Spannable.setSpan(new ForegroundColorSpan(Color.rgb()), 0, commentUsername1Spannable.length(), 0);
builder.append(commentUsername1Spannable);
builder.append(" ");
builder.append(text);
commentText1.setText(builder, TextView.BufferType.SPANNABLE);
}
尝试只使用一个文本视图:
user = (TextView) findViewById(R.id.commentUser1);
user.setText(Html.fromHtml(getString(R.string.commentUserName)));
user.setMovementMethod(LinkMovementMethod.getInstance());
并将字符串定义为:
服务水平
德克托斯·西莫拉多斯之子阿奎洛斯·德克托斯·奎恩·奥森西亚
明确的文本,以替代未来的内容
现场公共设施的生产
网状物También son llamados textos dummy,virtualcios o de relleno.
请原谅图片的大小,你能看看我上面第二次更新的答案吗?我无法使用HTML或XML,因为字符串正在从我的服务器加载。有没有办法直接修改新连接字符串的第一部分的样式?这是我们感兴趣的一行:新对象[]{commentUser1.getText().toString()。例如,我可以将字符串的这部分设置为粗体和绿色,或者更好的设置为不可见吗?