安卓绝对布局

发布于 2024-11-16 09:35:21 字数 1524 浏览 9 评论 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);

I have a complex timetable to layout using TextViews. I see that absolute layout has been depreciated. Now i know it is "bad practice" but i have a well formed screen metrics class that will adjust everything in relation to screen size/density etc.

If i cannot use absolute layout, then how can i tell the TextView I want it so much left and top? (like in HTML using an absolute position)?

I want to do this programmatically not using XML. Is this possible or do i have to use a WebView or canvas?

EDIT::

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

}

This is the method i am calling from this quick testcode:

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

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

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

发布评论

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

评论(2

时光与爱终年不遇 2024-11-23 09:35:22

好吧,经过整整 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);
}

Ok after a whole 24 hours of headscratching i finally came up with an answer for myself and anyone else :D
Thanks to everyone, your input certainly put me in right direction!

Basically margins in framelayout DO NOT WORK properly unless i am missing something. So what i have done is have to extend a fully expanded linear layout with the required view margined inside it.

For example:

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

}

This can then be added to the FrameLayout and will display the view in the right position.

Examples:

        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);
}
雨的味道风的声音 2024-11-23 09:35:21

是的,您可以使用以下代码以编程方式完成此操作。

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

Yes you can do it programmatically by using the following code..

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