【问题标题】:TextView write text vertically when setting its layoutparams to fill_parentTextView 在将其 layoutparams 设置为 fill_parent 时垂直写入文本
【发布时间】:2012-09-18 03:12:33
【问题描述】:

我正在尝试将TextView 添加到扩展LinearLayout 的视图中。当我将 layoutparams 的宽度设置为fill_parentwrap_content 时,TextView 内的文本将垂直显示,如下所示。

例如

 T
 E
 X
 T

但是,当我将宽度设置为某个固定数字时,它会正常显示或按我的预期显示。

例如正文

我的问题是为什么会发生这种情况以及如何解决它,即我应该如何以编程方式设置,以便TextView 可以在不设置固定宽度的情况下水平写入文本,例如设置fill_parentwrap_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


【解决方案1】:

LinearLayout 中似乎只有一个项目。请将 LinearLayout 的方向设置为"horizontal",如下所示:

android:orientation="horizontal"

此外,您应该尽可能从当前布局中获取 LayoutParams 并对其进行修改,而不是从头开始创建新的 LayoutParams 对象,如下所示:

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ilViewPager.getLayoutParams();
lp.<change-any-values>;
ilViewPager.setLayoutParams(lp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-28
    • 2011-12-18
    • 1970-01-01
    • 2011-09-22
    • 2013-10-27
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多