addView(View)不向objectView添加对象

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了addView(View)不向objectView添加对象相关的知识,希望对你有一定的参考价值。

在下面的代码中,您将看到我以编程方式创建了所有布局。我已经设置了contentView并在contentView中添加了一个视图,其中包含2个按钮。

contentView正在设置并正常工作,但所有涉及.addView(View)而不是。

所有帮助表示赞赏。谢谢。

我的代码:

package com.idleappsinc.ampup;

import android.content.res.Resources;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.AutoTransition;
import android.transition.TransitionManager;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class MainActivity extends AppCompatActivity {

    int rootWidth = 411;
    int rootHeight = 731;

    RelativeLayout rootView;
    RelativeLayout container;
    Button createParty;
    Button joinParty;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        rootView = new RelativeLayout(this);
        RelativeLayout.LayoutParams rootViewLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        rootView.setId(R.id.rootView);
        rootView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorBackground));
        rootView.setLayoutParams(rootViewLayoutParams);

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        container = new RelativeLayout(this);
        RelativeLayout.LayoutParams containerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (getWidth() / rootHeight) * 220);
        container.setId(R.id.relativeLayoutContainer);
        container.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
        containerLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        container.setLayoutParams(containerLayoutParams);

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        createParty = new Button(new ContextThemeWrapper(getApplicationContext(), android.R.style.Widget_Material_Button_Borderless));
        RelativeLayout.LayoutParams createPartyButtonParams = new RelativeLayout.LayoutParams((getWidth() / rootWidth) * 150, (getHeight() / rootHeight) * 50);
        createParty.setId(R.id.createPartyButton);
        createParty.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorCreatePartyButton));
        createParty.setText(R.string.create_party);
        createParty.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        createParty.setAllCaps(false);
        createParty.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
        createPartyButtonParams.width = (getWidth() / rootWidth) * 150;
        createPartyButtonParams.height = (getHeight() / rootHeight) * 50;
        createPartyButtonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        createPartyButtonParams.topMargin = (getHeight() / rootHeight) + 50;
        createParty.setLayoutParams(createPartyButtonParams);

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        joinParty = new Button(new ContextThemeWrapper(getApplicationContext(), android.R.style.Widget_Material_Button_Borderless));
        RelativeLayout.LayoutParams joinPartyButtonParams = new RelativeLayout.LayoutParams((getWidth() / rootWidth) * 150, (getHeight() / rootHeight) * 50);
        joinParty.setId(R.id.joinPartyButton);
        joinParty.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorJoinPartyButton));
        joinParty.setText(R.string.join_party);
        joinParty.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        joinParty.setAllCaps(false);
        joinParty.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.white));
        joinPartyButtonParams.width = (getWidth() / rootWidth) * 150;
        joinPartyButtonParams.height = (getHeight() / rootHeight) * 50;
        joinPartyButtonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        joinPartyButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        joinPartyButtonParams.bottomMargin = (getHeight() / rootHeight) + 50;
        joinParty.setLayoutParams(joinPartyButtonParams);

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        rootView.addView(container);
        container.addView(createParty);
        container.addView(joinParty);
        setContentView(rootView);

        createParty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                expandCreatePartyButton();
            }
        });

        joinParty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }

    private void expandCreatePartyButton() {

        // Hide join party button
        beginTransition(250);
        joinParty.setVisibility(View.INVISIBLE);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) createParty.getLayoutParams();
        RelativeLayout.LayoutParams containerParams = (RelativeLayout.LayoutParams) container.getLayoutParams();

        // Expand and set the relative layout height
        containerParams.height = containerParams.height + (int) dpToPx(50);
        container.setLayoutParams(containerParams);

        // Expand and set the button to the correct width and height
        params.topMargin = 0;
        params.width = containerParams.width;
        params.height = containerParams.height;
        createParty.setLayoutParams(params);


    }

    public static float dpToPx(int dp) {
        return (dp * Resources.getSystem().getDisplayMetrics().density);
    }

    public static float pxToDp(int px) {
        return (px / Resources.getSystem().getDisplayMetrics().density);
    }

    public void beginTransition(int delay) {
        // Create a delay based on given parameters
        AutoTransition delayTime = new AutoTransition();
        delayTime.setDuration(delay);

        // Create transition
        //TransitionManager.beginDelayedTransition(container, delayTime);
    }

    public int getWidth() {
        return  Math.round(Resources.getSystem().getDisplayMetrics().widthPixels / Resources.getSystem().getDisplayMetrics().density);
    }

    public int getHeight() {
        return Math.round(Resources.getSystem().getDisplayMetrics().heightPixels / Resources.getSystem().getDisplayMetrics().density);
    }

}
答案

我相信这是重新排序你的代码的问题,我会这样尝试并希望它的工作原理让我们看看:

    setContentView(rootView);
    rootView.addView(container);
    container.addView(createParty);
    container.addView(joinParty);

如果这不起作用,请尝试一次附加一个视图以捕获存在问题的视图。

另一答案

每次表演

getWidth() / rootWidth

要么

getHeight() / rootHeight

你将较小的值除以较大的值,它是一个整数除法,因此结果始终为0。

您将所有孩子的0分配为宽度和高度。因此,即使正确添加它们,您也根本看不到它们。

查找布局参数的替代值或至少执行浮点除法。

例如:

joinPartyButtonParams.width = (int) (((float) getWidth() / rootWidth) * 150);
joinPartyButtonParams.height = (int) (((float) getHeight() / rootHeight) * 50);

等等...

另一答案

我已经弄明白了这个问题!在此之前,宽度始终是应该的,但是每次都返回0的高度。在调试之后,我已经意识到代码返回的高度是返回高度减去UIDepth(在我的情况下为48dp)因为我已经设置了包含它的高度,所以每次都返回0。感谢所有人的帮助!

以上是关于addView(View)不向objectView添加对象的主要内容,如果未能解决你的问题,请参考以下文章

viewgroup用addview添加的view不显示问题

动画以及View绘制中的addview实战

Android Window系列- windowmanager.addview源码解析(View的更新)

addView的相关问题

调用ViewGroup的addView之后,是怎样显示出来的

windowManager.addView怎么自定义动画