android的Java方法:layout_gravity
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2945315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java method for android:layout_gravity
提问by André Leit?o
I would like to know if is there a way to call android:layout_gravity
property from a Java method. I didn't found any method in Android documentation to do it. This is the picture of the layout I want to implement:
我想知道是否有办法android:layout_gravity
从 Java 方法调用属性。我没有在 Android 文档中找到任何方法来做到这一点。这是我要实现的布局的图片:
http://www.anddev.org/resources/image/2234
http://www.anddev.org/resources/image/2234
I know to do it through XML, as following:
我知道通过 XML 来做,如下:
<FrameLayout
xlmns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="<" />
<Button
android:layout_gravity="right|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=">" />
</FrameLayout>
But in my situation, I need to do it through Java code, because I'll implement another layout views dynamically. To avoid merging XML layout with Java code, I would prefer make all layout using Java.
但在我的情况下,我需要通过 Java 代码来完成,因为我将动态实现另一个布局视图。为了避免将 XML 布局与 Java 代码合并,我更愿意使用 Java 进行所有布局。
采纳答案by Nikolay Ivanov
Well as far as I understand you looking for thisIt is for FrameLayout for other layout see appropriate LayoutParams classes. For setting gravity like in your XML use something like:
好吧,据我所知,你正在寻找这个它是用于其他布局的 FrameLayout,请参阅适当的 LayoutParams 类。要在 XML 中设置重力,请使用以下内容:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,Gravity.LEFT | Gravity.CENTER_VERTICAL)
followed by addView
with newly constructed LayoutParams
其次是addView
新建LayoutParams
回答by André Leit?o
Maybe I misunderstand you, but here it is:
也许我误解了你,但它是:
http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)
http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)
回答by Dimitry K
Watchout! This wouldn't work with LinearLayout though. Because LinearLayout.LayoutParams(...) constructor is different from FrameLayout.LayoutParams(...) constructor. The third parameter is not gravity but weight.
小心!但这不适用于 LinearLayout。因为 LinearLayout.LayoutParams(...) 构造函数不同于 FrameLayout.LayoutParams(...) 构造函数。第三个参数不是重力而是重量。
LinearLayout.LayoutParams(int width, int height, float weight)
as opposed to
与
FrameLayout.LayoutParams(int width, int height, int gravity)
and this call, despite not producingcompiler error is actually wrong:
而这个调用,尽管没有产生编译器错误实际上是错误的:
lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT);
回答by all-ok
button.setBackgroundResource(R.drawable.sound_on);
button.setText("On");
button.setGravity(Gravity.CENTER_VERTICAL| Gravity.RIGHT);
button.invalidate();
回答by Lekensteyn
To change the layout_gravity
attribute for an existing view:
要更改layout_gravity
现有视图的属性:
Button button = (Button) findViewById(R.id.some_button);
FrameLayout.LayoutParams params;
params = (LayoutParams) button.getLayoutParams();
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
button.setLayoutParams(params);
The button is contained in a FrameLayout
which has a gravity
attribute corresponding to the layout_gravity
XML attribute. Documentation link.
该按钮包含在FrameLayout
具有gravity
与layout_gravity
XML 属性对应的属性的 a 中。文档链接。
回答by Ali Vizcaya
before adding the button, you should change the orientation of your layout
在添加按钮之前,您应该更改布局的方向
yourbutton.setGravity(Gravity.RIGHT);
LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
layout.gravity = Gravity.RIGHT;
yourbutton.setLayoutParams(layout);
yourlayout.setGravity(Gravity.RIGHT);
yourlayout.addView(yourbutton);