![](/img/trans.png)
[英]Android LayoutGravity with ScrollView, TableRow and TextView
[英]How do I set LAYOUTGRAVITY for a TextView in Android?
如何通過代碼設置TextView的layout_gravity?
我嘗試使用layoutparams沒有任何好結果。
new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, Gravity.LEFT | Gravity.CENTER_VERTICAL);
使用完之后,我的文字就會停留在左上角的標准位置。 寬度:FILL_PARENT或重力代碼都不做任何事情。
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT | Gravity.CENTER_VERTICAL)
你可以使用下面的代碼在代碼中使用重力:
Button button = (Button) findViewById(R.id.MyButtonId);
// need to cast to LinearLayout.LayoutParams to access the gravity field
LayoutParams params = (LayoutParams)button.getLayoutParams();
params.gravity = Gravity.BOTTOM;
button.setLayoutParams(params);
layout = (LinearLayout) findViewById(R.id.vendor_detail_lay);
LinearLayout linearLayout = new LinearLayout(getApplicationContext());
linearLayout.setOrientation(0);
txtveiw = new TextView(NewVendorDetailActivity.this);
txtveiw.setLayoutParams(Gravity.LEFT | Gravity.CENTER_VERTICAL);
txtveiw.setId(itemId);
txtveiw.setText(cursor.getString(cursor.getColumnIndex(DataBaseHelper.KEYNAME)));
linearLayout.addView(txtveiw);
layout.addView(linearLayout);
Android以編程方式設置TextView的重力您是否看到過此響應?
TextView tv = (TextView) findViewById(R.layout.text_view);
tv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
保持現有布局參數的最佳解決方案:
例如,如果父是FrameLayout,那么:
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(textView.getLayoutParams()); layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; textView.setLayoutParams(layoutParams);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.