0

我有一个复杂的时间表来使用TextViews. 我看到绝对布局已经贬值了。现在我知道这是“不好的做法”,但我有一个格式良好的屏幕指标类,可以调整与屏幕尺寸/密度等相关的所有内容。

如果我不能使用绝对布局,那么我怎么能告诉TextView我我想要它这么多的左上角?(就像在 HTML 中使用绝对位置一样)?

我想以编程方式执行此操作,而不是使用 XML。这是可能的还是我必须使用一个WebView或画布?

编辑::

    void positionView (TextView v, int x, int y, int w, int h){
    final ViewGroup.MarginLayoutParams lpt = new ViewGroup.MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);//v.getLayoutParams();
    //lpt.setMargins(x,y,x+w,y+h);
    lpt.height=h;
    lpt.width=w;
    lpt.topMargin=y;
    lpt.leftMargin=x;
    v.setLayoutParams(lpt);

}

这是我从这个快速测试代码中调用的方法:

        ScrollView dayViewContainer = new ScrollView(context);
    dayViewContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    FrameLayout dayView = new FrameLayout(context);
    dayView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    dayView.setBackgroundColor(0xFF00FF00)
    int lineHeight = 60;

    for(int i =0;i<11 ;i++){
        TextView hourView = new TextView(context);
        hourView.setText(""+i+":00");
         BrScreenMetrics.getScreenWidthPX()-20, lineHeight);
        positionView(hourView, 50, i*lineHeight, BrScreenMetrics.getScreenWidthPX()-20, lineHeight);
        dayView.addView(hourView);
        }

    dayViewContainer.addView(dayView);
    addView(dayViewContainer);
4

2 回答 2

1

是的,您可以使用以下代码以编程方式进行操作。

TextView title = ((TextView)findViewById(R.id.default_panel_title))
final ViewGroup.MarginLayoutParams lpt =(MarginLayoutParams)title.getLayoutParams();

lpt.setMargins(0,lpt.topMargin,lpt.rightMargin,lpt.bottomMargin);

title.setLayoutParams(lpt);
于 2011-06-20T12:30:00.303 回答
1

好吧,经过整整 24 小时的头疼后,我终于为自己和其他人找到了答案:D 感谢大家,您的意见肯定让我朝着正确的方向前进!

除非我遗漏了什么,否则框架布局中的边距基本上不能正常工作。所以我所做的是必须扩展一个完全扩展的线性布局,并在其中添加所需的视图。

例如:

public class BrPositionableTextView extends LinearLayout{

TextView text ;

public BrPositionableTextView(Context context, int x, int y, int w, int h) {
    super(context);

    setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    text = new TextView(context);
    text.setHeight(h);
    text.setGravity(Gravity.CENTER_VERTICAL); 
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(x, y, 0, 0);
    params.height = h;
    params.width = w;
    addView(text, params);
}

/**
 * @return the text
 */
public String getText() {
    return text.getText().toString();
}

/**
 * @param text the text to set
 */
public void setText(String text) {
    this.text.setText(text);
}

public void setMargins(int x, int y, int w, int h) {
    //not tested, may need to recreate textview...
    LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(x, y, x+w, y+h);
    text.setLayoutParams(params);
}

public void setBackgroundColor(int color)
{
    text.setBackgroundColor(color);
}

}

然后可以将其添加到 FrameLayout 并在正确的位置显示视图。

例子:

        ScrollView dayViewContainer = new ScrollView(context);
    dayViewContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    FrameLayout dayView = new FrameLayout(context);
    dayView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    dayView.setBackgroundColor(0xFF005000);
    dayView.setMinimumHeight(BrScreenMetrics.getScreenHeightMinusStatusBar());
    dayView.setMinimumWidth(BrScreenMetrics.getScreenWidthPX());
    int lineHeight = 60;

    for(int i =0;i<11 ;i++){
        BrPositionableTextView hourView = new BrPositionableTextView(
                context, 
                8, 
                i*lineHeight, 
                80, 
                lineHeight);

        hourView.setText(""+(i+7)+":00");

        dayView.addView(hourView);
        BrPositionableSeparator separator = new BrPositionableSeparator(
                context, 
                80, 
                (i*lineHeight)+((int)(lineHeight*0.5f)), 
                BrScreenMetrics.getScreenWidthPX()-117, 
                1);

        separator.setColor(0xFFFFFFFF);
        dayView.addView(separator);

    }
    BrPositionableTextView bigassBox= new BrPositionableTextView(context, 100,200,300,500);
    bigassBox.setBackgroundColor(0xFF0000FF);
    dayView.addView(bigassBox);

    dayViewContainer.addView(dayView);
    addView(dayViewContainer);
}
于 2011-06-20T22:21:36.543 回答