我試圖動態創建TableRow
對象並將它們添加到TableLayout
。 TableRow
對象有2個項目,TextView
和CheckBox
。 TextView
項目需要將其佈局權重設置爲1,以將CheckBox
項目推到最右側。以編程方式設置TextView的佈局權重
我無法找到有關如何以編程方式設置TextView
項目的佈局權重的文檔。
我試圖動態創建TableRow
對象並將它們添加到TableLayout
。 TableRow
對象有2個項目,TextView
和CheckBox
。 TextView
項目需要將其佈局權重設置爲1,以將CheckBox
項目推到最右側。以編程方式設置TextView的佈局權重
我無法找到有關如何以編程方式設置TextView
項目的佈局權重的文檔。
你必須使用TableLayout.LayoutParams
像這樣的東西:
TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
最後一個參數是重量。
我在解決方案上遇到了相當多的困難:嘗試在TableRow中使用兩個按鈕,每個按鈕都是屏幕寬度的一半。無論出於何種原因,左側的按鈕總是大約70%的寬度,而右側的按鈕總是大約30%。調用table_layout.setStretchAllColumns(true)沒有效果,也沒有將按鈕的寬度設置爲屏幕的一半,也沒有設置它們的佈局權重。
我最終解決的解決方案是在TableRows中嵌套LinearLayout,確實在中考慮了按鈕寬度的值。
TableLayout layout = new TableLayout(this);
TableRow top_row = new TableRow(this);
left_button = styleButton();
right_button = styleButton();
LinearLayout toprow_layout = new LinearLayout (this);
toprow_layout.setOrientation(LinearLayout.HORIZONTAL);
toprow_layout.addView (left_button);
toprow_layout.addView(right_button);
toprow.addView(top_layout);
layout.addView(top_row)
private Button styleButton() {
Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
btn.setWidth((int)(display.getWidth()/2)); // set width to half
btn.setHeight(((int)display.getHeight()/6)); // set height to whatevs
btn.setText("foo");
return btn;
}
爲了您的未來,解決方案是將TableRow,Button1和Button2都設置爲'layout_width = fill_parent',在完成後,爲button1和button2設置'layout_weight = 1'。所有具有fill_parent和layout_weight = 1的元素在通貨膨脹時將被賦予相同的大小。 – edthethird 2012-03-02 18:00:36
答案是,你必須使用TableRow.LayoutParams,不LinearLayout.LayoutParams或任何其他的LayoutParams。
TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
不同的LayoutParams是不可互換的,如果你使用錯誤的,那麼似乎什麼都不會發生。該文本視圖的父表中的行,因此:
http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html
這個答案幫了我很多... TableLayout沒有爲我工作 – 2014-09-18 09:40:58
唉,你爲我節省了很多時間。它在哪裏指定?我沒有發現任何關於你的文檔不能交換'LayoutParams' – 2015-01-07 19:45:56
'TableRow.LayoutParams'也爲我工作 – 2015-02-08 22:54:30
這項工作對我來說,我希望它會爲你工作也
設置的LayoutParams父視圖第一:
myTableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.FILL_PARENT));
然後TextView的(孩子)設置:
TableLayout.LayoutParams textViewParam = new TableLayout.LayoutParams
(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT,1f);
//-- set components margins
textViewParam.setMargins(5, 0, 5,0);
myTextView.setLayoutParams(textViewParam);
剛剛成立的佈局PARAMS在佈局像
創建PARAM變量
android.widget.LinearLayout.LayoutParams params = new android.widget.LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
1F是權重參數
設置像
TextView text = (TextView) findViewById(R.id.text);
text.setLayoutParams(params);
你的部件或佈局還有另一種方式來做到這一點。如果您需要設置唯一的一個參數,例如「高度」:
TextView textView = (TextView)findViewById(R.id.text_view);
ViewGroup.LayoutParams params = textView.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
textView.setLayoutParams(params);
注意,不是爲2.3 – djdance 2016-09-28 20:35:47
您也可以單獨給重這樣,
LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
lp1.weight=1;
在前面的答案重量被傳遞到一個新的SomeLayoutType.LayoutParams對象的構造函數。 仍然在許多情況下,它更方便地使用現有的對象 - 它有助於避免與參數處理我們不感興趣的
一個例子:
// Get our View (TextView or anything) object:
View v = findViewById(R.id.our_view);
// Get params:
LinearLayout.LayoutParams loparams = (LinearLayout.LayoutParams) v.getLayoutParams();
// Set only target params:
loparams.height = 0;
loparams.weight = 1;
v.setLayoutParams(loparams);
TextView text = new TextView(v.getContext());
text.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1f));
(OR)
TextView tv = new TextView(v.getContext());
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
tv.setLayoutParams(params);
。
1f表示權重= 1;根據你的需要,你可以給2f或3f,視圖會根據空間移動。爲了在「線性佈局」中爲視圖之間指定距離,請使用「LinearLayout」的權重。
LinearLayout ll_Outer= (LinearLayout) view.findViewById(R.id.linearview);
LinearLayout llInner = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FillParent, LinearLayout.LayoutParams.WrapContent);
llInner.Orientation = Orientation.Horizontal;
llInner.WeightSum = 2;
ll_Outer.AddView(llInner);
TextView txtview = new TextView(v.getContext());
LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
txtview.setLayoutParams(params);
1F是表示作爲權重= 1;你可以給2F或3F,意見將移動accoding的空間
這應該工作到您
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT LayoutParamsMATCH_PARENT);
param.weight=1.0f;
之前我沒有提到這一點,所以我很抱歉。但我在詢問我的問題之前嘗試了這一點。它使TextView從佈局中消失。但是,從積極的角度來看,我發現將TableLayout的stretch_columns屬性設置爲0會導致我正在查找的效果(TextView在左邊,CheckBox在右邊)。 感謝您的幫助。 – eugene 2010-07-12 00:35:17
如果你正在導入一個XML佈局,並且你沒有動態地創建你的佈局,你可以像這樣抓住它:'TextView tv =(TextView)findViewById(R.id.your_textview_inside_the_linearlayout); tv.setLayoutParams(新的LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0.1f));' – 2012-04-13 20:51:13
要以編程方式設置佈局權重,應該使用'TableRow.LayoutParams'而不是'TableLayout.LayoutParams'。見@多傑的回答。 – Halil 2014-08-28 12:56:36