【问题标题】:Android sliding tabs - 2 line text on tabsAndroid 滑动标签 - 标签上的 2 行文本
【发布时间】:2014-12-30 16:38:59
【问题描述】:

我在网上看到这张图片,我想知道:是否可以在我的滑动标签项目中为我的一个标签提供两行文本视图?

Hello 标签错误

DeafultTabView 代码

protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setTextColor(context.getResources().getColor(R.color.black));

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    textView.setWidth(size.x / 2); // (size.x / number of textviews);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
                outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

【问题讨论】:

    标签: java android android-layout android-studio


    【解决方案1】:

    在从createDefaultTabView() 方法返回的TextView 中使用它

    myTextView.setText(Html.fromHtml("Line one <br/>Line two"));
    

    这样设置文本(通用条件)

    private void setCustomText(TextView textView, String[] msg, boolean isSingleLine){
        if(isSingleLine)
            textView.setText(msg[0]);
        else
            textView.setText(TextUtils.join("<br/>", msg));
    }
    

    【讨论】:

    • 我的滑动标签项目中有 2 个标签。我不想要它们两个的 2 行 - 我只想要其中 1 个选项卡的 2 行。
    • 4 个标签呢?第一个标签文本:你好;第二个标签文本:[1st line]Hello [2nd line]World;第三个标签文本:[1st line]Hello [2nd line]Europe;第 4 个标签文本:[1st line]Hello [2nd line]London.
    • 更新它以适应一般情况
    • 酷。虽然当我选择“你好”选项卡时,出现的深灰色框似乎并没有覆盖整个可触摸应用程序(红线上方出现一点浅灰色)。有没有办法解决这个问题?
    • 在 createDefaultTabView() 方法中调整 TextView 的布局参数。另外,如果它解决了您的问题,请接受它作为答案。 :-)
    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2018-02-18
    相关资源
    最近更新 更多