Reputation: 2053
below is a textview layout , and I hope to modify layout_marginRight to zero in code.
<TextView
android:id="@+id/status1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="@dimen/keyguard_lockscreen_status_line_font_right_margin"
android:singleLine="true"
android:ellipsize="marquee"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="@dimen/keyguard_lockscreen_status_line_font_size"
android:drawablePadding="4dip"
/>
when I copy aqif code to my code as below, phone halt at power on animation.
private void updateStatus1() {
if (mStatus1View != null) {
MutableInt icon = new MutableInt(0);
CharSequence string = getPriorityTextMessage(icon);
mStatus1View.setText(string);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mStatus1View.getLayoutParams();
params.rightMargin = 0;
mStatus1View.setLayoutParams(params);
mStatus1View.setCompoundDrawablesWithIntrinsicBounds(icon.value, 0, 0, 0);
mStatus1View.setVisibility(mShowingStatus ? View.VISIBLE : View.INVISIBLE);
}
}
Upvotes: 2
Views: 5007
Reputation: 3521
you can do it this way.
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.rightMargin = 30;
view.setLayoutParams(params);
to set Values in dp
you can do it like this.
params.rightMargin = (int) (30f * this.getResources().getDisplayMetrics().density);
and the type of params depends upon its parent, if your view parent is LinearLayout
the your params must be of LinearLayout.LayoutParams
type, and in case of RelativeLayout
, your params must be of RelativeLayout.LayoutParams
type.
regards, Aqif Hamid
Upvotes: 10
Reputation: 132982
try as by using ViewGroup.MarginLayoutParams.rightMargin:
TextView extView tv = (TextView)findViewById(R.id.status1);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
//params.setMargins(0, 0, 0, 0); //setMargins(int left, int top, int right, int bottom)
params.rightMargin=0;
tv.setLayoutParams(params);
Upvotes: 0