我在xml文件中定义了一个imagebutton。在运行时期间,我想让它可见(可以使用setVisibility()
,因此没有问题)但我也希望将其水平居中,就像XML属性android:layout_gravity="center_horizontal"
一样。你怎么在运行时,在javacode中做到这一点?我找不到任何方法用于以编程方式设置图像按钮的重力。
编辑:我在xml文件中使用RelativeLayout
EDIT2:XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center_horizontal">
<TextView android:text="Unknown" android:layout_width="wrap_content" android:id="@+id/screen_p2pcall_status_text" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_centerHorizontal="true"></TextView>
<ImageView android:layout_width="wrap_content" android:src="@drawable/android_avatar_big" android:id="@+id/screen_p2pcall_android" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_status_icon" android:layout_centerHorizontal="true" android:layout_marginTop="36dp"></ImageView>
<ImageView android:layout_width="wrap_content" android:src="@android:drawable/presence_invisible" android:id="@+id/screen_p2pcall_status_icon" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_status_text" android:layout_centerHorizontal="true"></ImageView>
<TextView android:text="Unknown" android:id="@+id/screen_p2pcall_peer" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_height="wrap_content" android:layout_below="@+id/screen_p2pcall_android" android:layout_centerHorizontal="true"></TextView>
<ImageButton android:layout_width="120dp" android:id="@+id/screen_p2pcall_hangup" android:src="@drawable/phone_hang_up_48" android:layout_height="50dp" android:layout_below="@+id/screen_p2pcall_peer" android:layout_toRightOf="@+id/screen_p2pcall_status_icon" android:layout_marginTop="18dp"></ImageButton>
<ImageButton android:layout_width="120dp" android:id="@+id/screen_p2pcall_answer" android:src="@android:drawable/sym_action_call" android:layout_height="50dp" android:layout_alignTop="@+id/screen_p2pcall_hangup" android:layout_toLeftOf="@+id/screen_p2pcall_hangup"></ImageButton>
</RelativeLayout>
java代码,我希望将其更改为在运行时水平居中@+id/screen_p2pcall_hangup
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_p2pcall);
final ImageButton hangup = (ImageButton)findViewById(R.id.screen_p2pcall_hangup);
// ????
}
由于
答案 0 :(得分:2)
使用相对布局时,您应该将布局参数的规则添加到居中子视图中心。 对于相对布局params对象,您应该添加像这样的规则
rlp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
可根据您的要求添加其他规则。 相对布局无法实现重力。
我假设这是活动的整个布局xml。如果是这样,这应该可行,
ImageButton hangupButton = (ImageButton)findViewById(R.id.screen_p2pcall_hangup);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 120, getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 50, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(width,height);
rlp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
hangupButton.setLayoutParams(rlp);
hangupButton.setVisibility(RelativeLayout.VISIBLE);
编辑:使用代码解决方案
我已经编辑了一些xml代码,以便在我的项目中进行测试。使用的属性不可用,例如“方向”。它仅适用于线性布局。下次发布问题时请将xml打破到下一行,这样我们就不需要左右滚动来阅读整个内容了。
layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:text="Unknown" android:layout_width="wrap_content"
android:id="@+id/screen_p2pcall_status_text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<ImageView android:layout_width="wrap_content"
android:src="@drawable/icon"
android:id="@+id/screen_p2pcall_android"
android:layout_height="wrap_content"
android:layout_below="@+id/screen_p2pcall_status_icon"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"/>
<ImageView android:layout_width="wrap_content"
android:src="@android:drawable/presence_invisible"
android:id="@+id/screen_p2pcall_status_icon"
android:layout_height="wrap_content"
android:layout_below="@+id/screen_p2pcall_status_text"
android:layout_centerHorizontal="true"/>
<TextView android:text="Unknown" android:id="@+id/screen_p2pcall_peer"
android:layout_width="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content"
android:layout_below="@+id/screen_p2pcall_android"
android:layout_centerHorizontal="true"/>
<ImageButton android:layout_width="120dp"
android:id="@+id/screen_p2pcall_hangup"
android:src="@android:drawable/sym_call_missed"
android:layout_height="50dp"
android:layout_below="@+id/screen_p2pcall_peer"
android:layout_toRightOf="@+id/screen_p2pcall_status_icon"
android:layout_marginTop="18dp"/>
<ImageButton android:layout_width="120dp"
android:id="@+id/screen_p2pcall_answer"
android:src="@android:drawable/sym_action_call"
android:layout_height="50dp"
android:layout_alignTop="@+id/screen_p2pcall_hangup"
android:layout_toLeftOf="@+id/screen_p2pcall_hangup"/>
</RelativeLayout>
现在我在onCreate方法中使用了这段代码,它隐藏了左下角按钮并水平居中右下角按钮
ImageButton hangupButton = (ImageButton)findViewById(R.id.screen_p2pcall_hangup);
int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 120, getResources().getDisplayMetrics());
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 50, getResources().getDisplayMetrics());
int marginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
(float) 18, getResources().getDisplayMetrics());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(width,height);
rlp.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
rlp.addRule(RelativeLayout.BELOW, R.id.screen_p2pcall_peer);
rlp.topMargin = marginTop;
hangupButton.setLayoutParams(rlp);
((ImageButton)findViewById(R.id.screen_p2pcall_answer))
.setVisibility(RelativeLayout.INVISIBLE);
这应该有用。