动态的改变Textview的margin属性

  假如有两个UI设计图,其中只有一个控件不太一样,其它的全都一样,这些是没有必要去重新去写两份布局文件的,在代码里动态设置控件不一样属性就好

private void updateScanDescTopMargin() {
    if (mTvScanDesc == null) {
        return;
    }
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mTvScanDesc.getLayoutParams();
    int newTop = DeviceUtil.dp2Px(56);
    if (isIconAnimScan()) {
        newTop = DeviceUtil.dp2Px(64);
    }
    layoutParams.setMargins(layoutParams.leftMargin, newTop, layoutParams.rightMargin, layoutParams.bottomMargin);
    mTvScanDesc.setLayoutParams(layoutParams);
}

mTvScanDesc为TextView, DevceUtil为 dp 转px 的工具类

  比较简单没什么值得注意的地方,唯一一点是TextView 本身是没有setMargins方法的 只有ViewGroup才有,所以需要给TextView 添加一层父布局。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
很抱歉,我无法直接提供完整的代码,因为这需要根据具体的实现细节进行编写。不过,我可以提供一份伪代码作为参考: 布局文件:activity_学号后3位.xml ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background_image"> <TextView android:id="@+id/question_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:textColor="#000000" android:text="问题:这里是问题的具体描述" /> <RadioButton android:id="@+id/option1_radiobutton" android:layout_below="@id/question_textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选项1" /> <RadioButton android:id="@+id/option2_radiobutton" android:layout_below="@id/option1_radiobutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选项2" /> <RadioButton android:id="@+id/option3_radiobutton" android:layout_below="@id/option2_radiobutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选项3" /> <Button android:id="@+id/submit_button" android:layout_below="@id/option3_radiobutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" /> </RelativeLayout> ``` Activity文件:Activity+学号后3位.java ``` public class Activity_学号后3位 extends AppCompatActivity { private RadioButton option1RadioButton; private RadioButton option2RadioButton; private RadioButton option3RadioButton; private Button submitButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_学号后3位); option1RadioButton = findViewById(R.id.option1_radiobutton); option2RadioButton = findViewById(R.id.option2_radiobutton); option3RadioButton = findViewById(R.id.option3_radiobutton); submitButton = findViewById(R.id.submit_button); submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (option1RadioButton.isChecked()) { // 答案正确的情况 Toast.makeText(Activity_学号后3位.this, "恭喜您,回答正确!", Toast.LENGTH_SHORT).show(); // 将结果存储在xml文件中 String result = "正确"; saveResultToXml(result); } else { // 答案错误的情况 Toast.makeText(Activity_学号后3位.this, "很遗憾,回答错误!", Toast.LENGTH_SHORT).show(); // 将结果存储在xml文件中 String result = "错误"; saveResultToXml(result); } } }); } private void saveResultToXml(String result) { String fileName = "姓名的拼音简称.xml"; // 将结果存储在xml文件中的代码 } } ``` 以上是一个简单的伪代码示例,你可以根据自己的需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值