4

I am trying to add TextView into a view which extends LinearLayout. When I set its width of layoutparams to fill_parent or wrap_content, the text inside TextView will display vertically such like the following.

e.g.

 T
 E
 X
 T

However, when I set the width to some fixed number, it will display normally or as I expected.

e.g. TEXT

My question is why this happens and how to solve it, i.e. how I should set programmatically so that TextView can write text horizontally without setting a fixed width to it such as setting fill_parent or wrap_content?

Here is setting of XML code of the parent ilGallery which extends LinearLayout:

<?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>

The following is the code how i initialize the children inside it:

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");
Goo
  • 1,318
  • 1
  • 13
  • 31
  • Please look at this [post](http://stackoverflow.com/questions/2888780/is-it-possible-to-write-vertically-in-a-textview-in-android), it's similar to your. – Artyom Kiriliyk Nov 05 '12 at 00:56

1 Answers1

0

There appears to be only a single item inside your LinearLayout. Please set the orientation of the LinearLayout to "horizontal", like this:

android:orientation="horizontal"

Also, whenever possible instead of creating new LayoutParams objects from scratch, you should get the LayoutParams from the current layout and modify that, like this:

LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ilViewPager.getLayoutParams();
lp.<change-any-values>;
ilViewPager.setLayoutParams(lp);
David Manpearl
  • 12,362
  • 8
  • 55
  • 72