日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android gravity参数,android - 如何以编程方式设置layout_gravity?

發布時間:2024/3/12 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android gravity参数,android - 如何以编程方式设置layout_gravity? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

android - 如何以編程方式設置layout_gravity?

我的問題很簡單,

如何以編程方式設置我的按鈕layout_gravity?

我在互聯網上發現了這個,但它只是拋出了一個Nullpointer異常:

Button MyButton = new Button(this);

LinearLayout.LayoutParams lllp=(LinearLayout.LayoutParams)MyButton.getLayoutParams();

lllp.gravity=Gravity.RIGHT;

MyButton.setLayoutParams(lllp);

MyLinearLayout.addView(MyButton);

有解決方案嗎

16個解決方案

468 votes

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);

params.weight = 1.0f;

params.gravity = Gravity.TOP;

button.setLayoutParams(params);

對于重力值以及如何設置重力檢查重力。

基本上,您應該根據父級選擇LayoutParams。 它可以是RelativeLayout,LinearLayout等......

Karthi answered 2019-02-15T05:20:53Z

50 votes

我不想復活舊線程,但這是一個無法正確回答的問題,而且我自己遇到了這個問題。

這里有點長,如果你只對答案感興趣,請一直向下滾動到代碼:

android:gravity和android:layout_gravity的工作方式不同。 這是我讀過的一篇幫助我的文章。

文章的GIST:重力影響高度/寬度分配后的視圖。 因此,重心不會影響完成FILL_PARENT的視圖(將其視為自動邊距)。 layout_gravity center將影響FILL_PARENT視圖(將其視為自動填充)。

基本上,android:layout_gravity不能以編程方式訪問,只有android:gravity。在OP的情況和我的情況下,接受的答案不會將按鈕垂直居中。

改進Karthi的答案:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

params.gravity = Gravity.CENTER;

button.setLayoutParams(params);

鏈接到LinearLayout.LayoutParams。

android:layout_gravity顯示“無相關方法”意思無法以編程方式訪問。而重力是班上的一個領域。

Uknight answered 2019-02-15T05:22:12Z

22 votes

MyButton.setGravity(Gravity.RIGHT);

對于layout_gravity,請使用“karthi”所述的答案。 此方法設置重力以將子項放在視圖中。

Yashwanth Kumar answered 2019-02-15T05:22:38Z

16 votes

我在GridLayout中按鈕上以編程方式設置layout_gravity時遇到了類似的問題。

訣竅是在按鈕被添加到父(GridLayout)之后在按鈕layoutParams上設置重力,否則將忽略重力。

grid.addView(button)

((GridLayout.LayoutParams)button.getLayoutParams()).setGravity(int)

owjsub answered 2019-02-15T05:23:12Z

8 votes

這個問題很老,但我遇到了同樣的問題并且像這樣解決了

LayoutParams lay = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)

lay.gravity = Gravity.CENTER;

CodeFlakes answered 2019-02-15T05:23:39Z

8 votes

layoutParams2.gravity = Gravity.RIGHT|Gravity.BOTTOM;

用它來增加一個以上的引力

Omar Alnajjar answered 2019-02-15T05:24:09Z

6 votes

如果要更改現有視圖的LayoutParams,請執行以下操作:

((FrameLayout.LayoutParams) view.getLayoutParams()).gravity = Gravity.BOTTOM;

請記住根據您的視圖所在的布局類型使用正確的LayoutParams。例如:

LinearLayout.LayoutParams

vovahost answered 2019-02-15T05:24:43Z

4 votes

我使用類似的東西:(Xamarin和C#代碼)

LinearLayout linLayout= new LinearLayout(this);

linLayout.SetGravity(GravityFlags.Center);

TextView txtView= new TextView(this);

linLayout.AddView(txtView);

SetGravity將我的textView放在布局的中心。所以SetGravity布局屬性指的是布局內容

alin0509 answered 2019-02-15T05:25:18Z

3 votes

如果您需要為View設置Gravity,請使用以下內容

Button b=new Button(Context);

b.setGravity(Gravity.CENTER);

用于設置Button的layout_gravity使用重力場作為布局參數

LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

lp.gravity=Gravity.CENTER;

試試這個希望這清楚謝謝

user2779311 answered 2019-02-15T05:25:59Z

3 votes

其余答案是對的,我想補充更多解釋。 layout_gravity是關于如何在父視圖中定位視圖。

在調用方法parentView.addView()**之后,必須設置重力**。 我們可以看到代碼:

public void setLayoutParams(ViewGroup.LayoutParams params) {

if (params == null) {

throw new NullPointerException("Layout parameters cannot be null");

}

mLayoutParams = params;

resolveLayoutParams();

if (mParent instanceof ViewGroup) {

((ViewGroup) mParent).onSetLayoutParams(this, params);

}

requestLayout();

}

而且空指針的問題是因為它沒有在getLayoutParams()之前調用addView。

注釋已經說過“如果此View未附加到父ViewGroup,或者@@ link#setLayoutParams(android.view.ViewGroup.LayoutParams)}未成功調用,則此方法可能返回null。當視圖附加到父項時 ViewGroup,此方法不能返回null。“

Snow Albert answered 2019-02-15T05:26:45Z

2 votes

到RelativeLayout,嘗試這個代碼,它適用于我:yourLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

michael wang answered 2019-02-15T05:27:11Z

2 votes

完美的工作! 上述答案都不適合我。 在Xml文件中設置重力和設置layout_gravity是不同的。 看看下面的代碼

// here messageLL is the linear layout in the xml file

// Before adding any view just remove all views

messageLL.removeAllViews();

// FrameLayout is the parent for LinearLayout

FrameLayout.LayoutParams params = new

FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

params.gravity = Gravity.CENTER|Gravity.CENTER_VERTICAL;

messageLL.setLayoutParams(params);

messageText.setVisibility(View.GONE);

messageNoText.setVisibility(View.VISIBLE);

messageLL.addView(messageNoText);

同時選中This,您可以在其中找到有關gravity和layout_gravity的清晰說明。

Tara answered 2019-02-15T05:27:48Z

2 votes

如果要將視圖放在父級的中心,可以使用以下代碼。

public class myLayout extends LinearLayout {

public myLayout(Context context) {

super(context);

RelativeLayout vi = (RelativeLayout) ((LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(

R.layout.activity_main, null);

LinearLayout.LayoutParams cc = new LinearLayout.LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

cc.gravity = Gravity.CENTER;

this.setGravity(Gravity.CENTER);

this.addView(vi);

}

}

這些代碼部分使LinearLayout將第一個視圖元素放在父級的中心。因此,我們系統不考慮初始寬度和高度來排列中心的視圖。我很好地完成了代碼部分。

Cheng answered 2019-02-15T05:28:23Z

0 votes

我從LinearLayout.LayoutParams切換到RelativeLayout.LayoutParams,最終得到我在我創建的自定義circleview上所需的結果。

但是使用addRule而不是引力

RelativeLayout.LayoutParams mCircleParams = new RelativeLayout.LayoutParams(circleheight,circleheight);

mCircleParams.addRule(RelativeLayout.CENTER_IN_PARENT);

user3886197 answered 2019-02-15T05:29:04Z

0 votes

試試這個代碼

Button btn = new Button(YourActivity.this);

btn.setGravity(Gravity.CENTER | Gravity.TOP);

btn.setText("some text");

要么

btn.setGravity(Gravity.TOP);

Faxriddin Abdullayev answered 2019-02-15T05:29:26Z

0 votes

int width=getResources().getDisplayMetrics().widthPixels;

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams

(width, width);

params.gravity = Gravity.CENTER;

iv_main_text = new HTextView(getContext());

iv_main_text.setLayoutParams(params);

iv_main_text.setBackgroundColor(Color.BLUE);

iv_main_text.setTextSize(60);

iv_main_text.setGravity(Gravity.CENTER);

iv_main_text.setTextColor(Color.BLACK);

Manthan Patel answered 2019-02-15T05:29:45Z

總結

以上是生活随笔為你收集整理的android gravity参数,android - 如何以编程方式设置layout_gravity?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。