iXcoder
iXcoder

Reputation: 1564

How to make the textview content HORIZONTAL

I need dynamic create the texview, it works just the text content show VERTICAL but HORIZONTAL

TextView tv = new TextView(this.theContext);
    tv.setText(this.getTip(0));
    tv.setBackgroundColor(Color.BLUE);
    tv.setGravity(Gravity.CENTER_HORIZONTAL);

    tv.setLayoutDirection(View.LAYOUT_DIRECTION_LTR);
    LinearLayout.LayoutParams params = 
            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 0);
        params.gravity = Gravity.CENTER;
        this.addView(tv, params);

    mLeftLabel = tv;

also, I set the layout :

tv.layout(l,t,r,b); . but I think that this statement should not effect the text Direction, is it right ?

please give help or tip, thanks.

Upvotes: 0

Views: 60

Answers (2)

Roadblock
Roadblock

Reputation: 2071

try changing LayoutParams as

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

setting gravity to center when you are using WRAP_CONTENT for textview does not seem to make sense, though I am not sure.

Upvotes: 0

Alexey
Alexey

Reputation: 4491

try to follow the example below:

TextView tv = new TextView(this);
tv.setText("text");
LinearLayout.LayoutParams params = 
    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
params.gravity = Gravity.CENTER;
mainLayout.addView(tv, params);

Upvotes: 1

Related Questions