自定义首选项中的搜索栏宽度

发布于 2024-09-15 07:12:00 字数 2392 浏览 1 评论 0原文

我正在尝试编写一个首选项小部件以使用 SeekBar 设置值。这包括标题、摘要、搜索栏和用于显示当前值的文本视图。除了标题之外的所有内容都应显示在第二行中(作为标准首选项)。

我无法让它发挥作用。我首先尝试使用relativelayout,但下面等规则未应用。然后我尝试使用线性布局+tablelayout,但Seekbar没有捕获minimumWidth并且仍然很小。

image 此处

所有这些都是通过代码完成的。这是

 LinearLayout layoutv = new LinearLayout(getContext());
    layoutv.setOrientation(LinearLayout.VERTICAL);

    //Title
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    TextView view = new TextView(getContext());
    view.setId(VIEW_ID_TITLE);
    view.setText(getTitle());
    view.setTextSize(21);
    view.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
    view.setTextColor(Color.WHITE);
    view.setLayoutParams(params1);
    layoutv.addView(view);

    //TAble Layout to show all the information
    TableLayout layout = new TableLayout(getContext());

    TableLayout.LayoutParams params0 = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.FILL_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(params0);
    layout.setShrinkAllColumns(true);

    //Row
    TableRow tr = new TableRow(getContext());

    //Summary
    TextView summary = new TextView(getContext());
    summary.setId(VIEW_ID_SUMMARY);
    summary.setText(getSummary());
    summary.setTextSize(14);
    summary.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
    tr.addView(summary);

    //SeekBar
    SeekBar bar = new SeekBar(getContext());
    bar.setId(VIEW_ID_BAR);
    bar.setMax(maximum);
    bar.setProgress((int)this.oldValue);
    bar.setMinimumWidth(80);
    bar.setOnSeekBarChangeListener(this);
    tr.addView(bar);

    //Monitor Box
    this.monitorBox = new TextView(getContext());
    this.monitorBox.setId(VIEW_ID_MONITOR);
    this.monitorBox.setTextSize(12);
    this.monitorBox.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC);
    this.monitorBox.setPadding(2, 5, 0, 0);
    this.monitorBox.setText(Float.toString((float) bar.getProgress()/10.0f));
    tr.addView(this.monitorBox);       

    layout.addView(tr);

   layoutv.setPadding(15, 5, 10, 5); 

   layoutv.addView(layout);

   layoutv.setId(android.R.id.widget_frame);

   return layoutv;

非常感谢

I'm trying to code a preferences widget to set a value with a SeekBar. This includes a Title, Summary, Seekbar and a TextView to show current value. All of them but Title should be shown in a second row (as standard preferences).

I can't make it work. I first tried with a RelativeLayout but BELOW, etc. rules where not applied.Then I'm trying with Linear Layout + TableLayout but Seekbar doesn't catch minimumWidth and remains small.

image here

All this is done through code. Here it is

 LinearLayout layoutv = new LinearLayout(getContext());
    layoutv.setOrientation(LinearLayout.VERTICAL);

    //Title
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    TextView view = new TextView(getContext());
    view.setId(VIEW_ID_TITLE);
    view.setText(getTitle());
    view.setTextSize(21);
    view.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
    view.setTextColor(Color.WHITE);
    view.setLayoutParams(params1);
    layoutv.addView(view);

    //TAble Layout to show all the information
    TableLayout layout = new TableLayout(getContext());

    TableLayout.LayoutParams params0 = new TableLayout.LayoutParams(
            TableLayout.LayoutParams.FILL_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(params0);
    layout.setShrinkAllColumns(true);

    //Row
    TableRow tr = new TableRow(getContext());

    //Summary
    TextView summary = new TextView(getContext());
    summary.setId(VIEW_ID_SUMMARY);
    summary.setText(getSummary());
    summary.setTextSize(14);
    summary.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
    tr.addView(summary);

    //SeekBar
    SeekBar bar = new SeekBar(getContext());
    bar.setId(VIEW_ID_BAR);
    bar.setMax(maximum);
    bar.setProgress((int)this.oldValue);
    bar.setMinimumWidth(80);
    bar.setOnSeekBarChangeListener(this);
    tr.addView(bar);

    //Monitor Box
    this.monitorBox = new TextView(getContext());
    this.monitorBox.setId(VIEW_ID_MONITOR);
    this.monitorBox.setTextSize(12);
    this.monitorBox.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC);
    this.monitorBox.setPadding(2, 5, 0, 0);
    this.monitorBox.setText(Float.toString((float) bar.getProgress()/10.0f));
    tr.addView(this.monitorBox);       

    layout.addView(tr);

   layoutv.setPadding(15, 5, 10, 5); 

   layoutv.addView(layout);

   layoutv.setId(android.R.id.widget_frame);

   return layoutv;

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

财迷小姐 2024-09-22 07:12:00

这是RelativeLayout 的代码。两个文本重叠显示在同一行中

RelativeLayout layout = new RelativeLayout(getContext());

//Title
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView view = new TextView(getContext());
view.setId(VIEW_ID_TITLE);
view.setText(getTitle());
view.setTextSize(21);
view.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
view.setTextColor(Color.WHITE);

layout.addView(view, params1);

//Summary
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView summary = new TextView(getContext());
summary.setId(VIEW_ID_SUMMARY);
summary.setText(getSummary());
summary.setTextSize(14);
summary.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
params2.addRule(RelativeLayout.BELOW, VIEW_ID_TITLE);
params2.addRule(RelativeLayout.ALIGN_LEFT, VIEW_ID_TITLE);  
layout.addView(summary, params2);


layout.setPadding(15, 5, 10, 5);  

layout.setId(android.R.id.widget_frame);

return layout;

This is the code for RelativeLayout. Both texts are shown in overlapped in the same line

RelativeLayout layout = new RelativeLayout(getContext());

//Title
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView view = new TextView(getContext());
view.setId(VIEW_ID_TITLE);
view.setText(getTitle());
view.setTextSize(21);
view.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
view.setTextColor(Color.WHITE);

layout.addView(view, params1);

//Summary
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView summary = new TextView(getContext());
summary.setId(VIEW_ID_SUMMARY);
summary.setText(getSummary());
summary.setTextSize(14);
summary.setTypeface(Typeface.SANS_SERIF, Typeface.NORMAL);
params2.addRule(RelativeLayout.BELOW, VIEW_ID_TITLE);
params2.addRule(RelativeLayout.ALIGN_LEFT, VIEW_ID_TITLE);  
layout.addView(summary, params2);


layout.setPadding(15, 5, 10, 5);  

layout.setId(android.R.id.widget_frame);

return layout;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文