簡體   English   中英

在TextView上使用setMaxLines顯示行,但不顯示行中的文本

[英]Using setMaxLines on a TextView shows lines but not text in lines

我有一個從服務器獲取菜單並將其解析為TextViews和按鈕的應用程序,這樣做我有時名稱較長,因此我想顯示名稱的前三行(如果有那么多行),問題是當我將maxLines設置為3,我看到3行,但第一行中只有文本,奇怪的是,當我將setLines設置為3時,我可以看到3行和文本,但是比起我有3行來表示僅1行長。

碼:

    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);
}

視圖參數:

    /* 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);

照片:

看不到文字

對不起,我的英語不好,謝謝!

下一行似乎有問題:

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

由於某種原因(我不確定為什么,可能是一個錯誤),它不起作用。 您只能添加參數,而“相對”布局會將文本視圖放在布局的左側,您將看到文本。

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM