我有一個垂直方向的LinearLayout
,我試圖給它添加三個子視圖。一個視圖是一個小50px高的視圖,只是在其中繪製一個框,第二個視圖是單選按鈕的水平列表,第三個視圖是圖。無論訂單添加到佈局如何製作合適的佈局視圖?
如果我最後添加圖形,所有內容都適合屏幕顯示,但如果我先添加圖形,它會將所有內容都推離屏幕,併成爲屏幕上唯一的視圖。該圖是我的一個自定義控件,它想要儘可能大(它的onMeasure
函數只是返回提供的寬度和高度),我認爲這是問題所在。但是我希望儘可能保持它的大小,並且無論訂單的順序如何,佈局都適合視圖。
這可能嗎?如果是這樣,我該如何實現它?最終,我試圖將圖形放在最上面,其他兩個控件放在它下面,但是如果我先添加圖形,它會消耗整個屏幕。
下面是我用來創建視圖的代碼。這是一個學校作業,我無法使用XML佈局。我已經被分配去做一個控制,我已經完成了。我只是想做一個簡單的活動來顯示控制權。
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(new MovingBox(this));
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
radioGroup.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
RadioButton button = new RadioButton(this);
button.setText("Light");
button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
radioGroup.addView(button);
button = new RadioButton(this);
button.setText("Dark");
button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
radioGroup.addView(button);
button = new RadioButton(this);
button.setText("Colorful");
button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
radioGroup.addView(button);
layout.addView(radioGroup);
// If I move these next to lines just below layout.setOrientation(...) it becomes the only thing visible
TweenerControl tw = new TweenerControl(this);
layout.addView(tw);
setContentView(layout);
}
}
添加最後的圖形:(一切都適合,但我想在上面的圖)
第一添加圖表:(只圖可見)
你可以發佈你正在使用的XML佈局嗎? – brianestey
@brianestey:目前沒有XML。我將編輯它以發佈代碼。我無法將XML用於此作業。 '編輯'好了,添加了代碼。 – Cornstalks
考慮使用'layout_weight','layout.addView(tw,new LayoutParams(LayoutParams.MATCH_PARENT,0,1));' – Aprian