addView(View)不向objectView添加对象

时间:2017-12-19 21:54:08

标签: android layout views subviews layoutsubviews

在下面的代码中,您将看到我已经以编程方式创建了所有布局。我已经设置了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);
    }

}

3 个答案:

答案 0 :(得分:0)

每次执行

getWidth() / rootWidth

getHeight() / rootHeight

您为一个较大的值划分了一个小值,它是整数除法,因此结果总是 0

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

为布局参数找到替代值,或者至少执行浮点划分

例如:

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

依旧......

答案 1 :(得分:0)

我认为这是重新排序代码的问题, 我会这样试试,希望它有用,让我们看看:

Arrays.sort(accounts, Comparator.nullsLast(Comparator.comparing(Account::getAccountNumber)));

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

答案 2 :(得分:0)

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