在以下代码中,您会看到我已经通过编程方式创建了我的布局。我已经设置了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
您正在为更大的值分配一个小值,它是 Integer Division ,因此结果始终为 0 。
您正在分配给所有孩子 0作为宽度和高度。因此,即使正确添加了它们,您也不会看到它们。
查找布局参数的替代值或至少执行 float Discor 。
例如:
joinPartyButtonParams.width = (int) (((float) getWidth() / rootWidth) * 150);
joinPartyButtonParams.height = (int) (((float) getHeight() / rootHeight) * 50);
等...
我已经弄清楚了问题!以前,宽度总是应有的,但是每次都返回0的高度。调试后,我意识到代码返回的高度正在返回高度(在我的情况下为48DP),因为我设置了高度以包括在内,每次都返回0。感谢所有人的帮助!