我不确定它只是"最后一行",但我们有一个带有fill_parent宽度,wrap_content高度的TextView的应用程序.文本正在从Java代码中动态放入.即使布局中有足够的空间,文本的最后一行也没有显示出来.它在一个相当深的视图层次结构内部,所以我的猜测是它的测量逻辑变得混乱,但它非常令人沮丧.我们需要猜测文本中有多少行,并相应地设置'android:lines'以使其工作.
有谁见过这个?在代码中,请参见底部的id'contentTextView'.如果我拿出'android:lines',最后一行文字就会消失.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/flag"
>
<include android:id="@+id/incHeader" layout="@layout/header"/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:layout_width="fill_parent"
android:layout_height="101dp"
android:background="@drawable/headershadow"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_weight="1"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="101dp"
android:background="@drawable/footershadow"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gybcontentback"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<include android:id="@+id/gybHeaderInclude" layout="@layout/gybcontentheader"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#4f4f4f"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:lines="9"
android:id="@+id/contentTextView"/>
<ImageView
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_blue_rascal_button"
android:id="@+id/buttonMoreInfo"/>
</LinearLayout>
</FrameLayout>
<include android:id="@+id/incFooter" layout="@layout/menu"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Java代码.我在onCreate期间使用标准Java字符串填充TextView.
@Override
protected String body()
{
return "Rascal Flatts and The Jason Foundation, Inc. are working together to prevent suicide.\n\n" +
"Your Battle Buddy (or family member) may need a friend.\n\n" +
"Take the pledge to B1.";
}
Run Code Online (Sandbox Code Playgroud)
基类调用它来获取实际文本
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(contentView());
wireMenu();
TextView headerText = (TextView) findViewById(R.id.gybHeaderText);
if(headerText != null)
headerText.setText(header());
((TextView) findViewById(R.id.contentTextView)).setText(body()); // This is where the text is set
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我把它煮沸了.我正在切割零件以查看剩下的内容并仍然遇到同样的问题.我想我可能找到了触发器和解决方案,但不是"原因".
我将布局归结为它的基础部分。这是精简版本,仍然删除了文本:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/flag"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#4f4f4f"
android:id="@+id/contentTextView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_blue_rascal_button"
android:id="@+id/buttonMoreInfo"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我认为问题在于父 LinearLayout 从其内部的 ImageView 获取其宽度。在这种组合中的某个地方,TextView 获取的其请求高度的值不正确。我想这与 UI 的计算方式有关。家长必须询问孩子们要求的宽度,然后回去告诉每个人他们的宽度是多少,然后询问高度。类似的事情。无论如何,以下工作。图像宽度为263像素(以mdpi为单位),因此如果我在父布局上设置263dp而不是wrap_content,则一切正常。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/flag"
>
<LinearLayout
android:layout_width="263dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#4f4f4f"
android:id="@+id/contentTextView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_blue_rascal_button"
android:id="@+id/buttonMoreInfo"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
将 TextView 设置为 wrap_content 也有效,尽管它会将整个布局推得比我想要的更大。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/flag"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#4f4f4f"
android:id="@+id/contentTextView"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button_blue_rascal_button"
android:id="@+id/buttonMoreInfo"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
不确定这里的要点是什么。总之,如果您开始看到计算的高度问题,请考虑修复父级的宽度,这样 UI 计算就不会混淆。
| 归档时间: |
|
| 查看次数: |
9251 次 |
| 最近记录: |