2

下面是一个 textview 布局,我希望在代码中将 layout_marginRight 修改为零。

<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"
 />

当我将 aqif 代码复制到我的代码如下时,手机在开机动画时暂停。

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);
        }
    }
4

2 回答 2

10

你可以这样做。

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.rightMargin = 30;
view.setLayoutParams(params);

设置值dp你可以这样做。

params.rightMargin = (int) (30f * this.getResources().getDisplayMetrics().density);

并且参数的类型取决于其父级,如果您的视图父级是LinearLayout您的参数必须是LinearLayout.LayoutParams类型,如果是RelativeLayout,您的参数必须是RelativeLayout.LayoutParams类型。

问候, 阿基夫·哈米德

于 2012-06-30T10:51:11.970 回答
0

尝试使用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);
于 2012-06-30T10:55:44.697 回答