【发布时间】:2012-09-18 03:12:33
【问题描述】:
我正在尝试将TextView 添加到扩展LinearLayout 的视图中。当我将 layoutparams 的宽度设置为fill_parent 或wrap_content 时,TextView 内的文本将垂直显示,如下所示。
例如
T
E
X
T
但是,当我将宽度设置为某个固定数字时,它会正常显示或按我的预期显示。
例如正文
我的问题是为什么会发生这种情况以及如何解决它,即我应该如何以编程方式设置,以便TextView 可以在不设置固定宽度的情况下水平写入文本,例如设置fill_parent 或wrap_content?
这里是父ilGallery扩展LinearLayout的XML代码设置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.illib.ilgallery.view.ILGallery
android:id="@+id/galleryLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
以下是我如何初始化其中的孩子的代码:
ilViewPager = new ILViewPager(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
ilViewPager.setLayoutParams(params);
ilgallery = this;
//initialize default header and footer view
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
headerView = new TextView(context);
headerView.setLayoutParams(params2);
((TextView)headerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)headerView).setText("hello");
footerView = new TextView(context);
footerView.setLayoutParams(params2);
((TextView)footerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)footerView).setText("hello2");
【问题讨论】:
-
请看这个post,和你的很像。
标签: android android-layout textview