简体   繁体   中英

Using setMaxLines on a TextView shows lines but not text in lines

I have an app that takes a menu from server and parse it to TextViews and buttons, doing that i sometime have longer names, so i want to present the 3 first lines of name(if it has that many), the problem is that when i set maxLines to 3, i see the 3 lines but only the text in the first line, the weird thing is when i use setLines to 3, i can see 3 lines and text, but than i have 3 lines for names that are only 1 line long.

Code:

    private void createItemView(Item itemToAdd) {


    /* Constructing a layout panel for all views */
    RelativeLayout viewPanel = new RelativeLayout(this);

    /* Creating view for the name of item */
    TextView itemName = new TextView(this);
    itemName.setTextColor(getResources().getColor(android.R.color.white));
    itemName.setTextSize(itemToAdd.getNameSize());

    itemName.setSingleLine(false);
    itemName.setWidth(300);
    itemName.setMaxLines(3);
    itemName.setText(itemToAdd.getName());

    /* Creating add button view */
    TextView addButton = new TextView(this);
    addButton.setTextColor(getResources().getColor(android.R.color.white));
    addButton.setTextSize(itemToAdd.getNameSize() + 5);
    addButton.setText("+");

    /* Creating price view */
    TextView priceView = new TextView(this);
    priceView.setTextColor(getResources().getColor(android.R.color.white));
    priceView.setTextSize(itemToAdd.getNameSize());
    priceView.setText(itemToAdd.getPrice());

    /* Adding  views to panel */
    viewPanel.addView(addButton, rightParams);
    viewPanel.addView(priceView, centerParams);
    viewPanel.addView(itemName, leftParams);

    /* adding view panel to layout */
    menuLayout.addView(viewPanel);
}

Params for views:

    /* Constructing Linear layout here */
    menuLayout = new LinearLayout(this);
    menuLayout.setOrientation(LinearLayout.VERTICAL);
    menuLayout.setBackgroundColor(getResources().getColor(android.R.color.background_dark));

    /* Constructing rules for relative layout here */
    rightParams =  new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    leftParams =  new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    centerParams =  new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    /* Adding rules */
    rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    centerParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);

Photo:

看不到文字

I am sorry for my bad english,Thank you!

Seems like this next line has a problem:

        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

for some reason(i'm not sure why, maybe a bug) its not working. You can just not add params, and Relative layout will put your text view on the left side of the layout, and you will see the text.

hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM