android:layout_marginRight 不起作用解决方法

本文介绍了解决在RelativeLayout中设置按钮时遇到的问题。当尝试在右侧放置按钮并使用marginRight属性时,发现按钮并未如预期般排列。通过添加android:layout_alignParentRight=true属性,成功实现了正确的布局。

今天想在RelativeLayout的左右分别放上一个按钮,

左边按钮用marginLeft=”10dp”,空出了10dp的距离,没问题.

但是右边的按钮marginRight=”10dp”就是过不去.

试了很久,最后在这个代码前加了一条android:layout_alignParentRight=”true”

解决了问题!!!

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent" app:cardCornerRadius="8dp"> <!-- 数据卡片 --> <GridLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="2" android:padding="16dp" > <!-- 左侧网格(第一列) --> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <!-- 属性名 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电机状态:" android:textStyle="bold" /> <View android:id="@+id/djStateO" android:layout_width="18dp" android:layout_height="18dp" android:layout_centerVertical="true" android:layout_marginRight="8dp" android:background="@drawable/circle_red" /> <!-- 属性值 --> <TextView android:id="@+id/djState" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未使能" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <!-- 属性名 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="遥控器状态:" android:textStyle="bold" /> <View android:id="@+id/ykqStateO" android:layout_width="18dp" android:layout_height="18dp" android:layout_centerVertical="true" android:layout_marginRight="8dp" android:background="@drawable/circle_red" /> <!-- 属性值 --> <TextView android:id="@+id/ykqState" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="不在线" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <!-- 属性名 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="防撞条状态:" android:textStyle="bold" /> <View android:id="@+id/fztStateO" android:layout_width="18dp" android:layout_height="18dp" android:layout_centerVertical="true" android:layout_marginRight="8dp" android:background="@drawable/circle_green" /> <TextView android:id="@+id/fztState" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="无撞击" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <!-- 属性名 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="球蓝状态:" android:textStyle="bold" /> <View android:id="@+id/qlStateO" android:layout_width="18dp" android:layout_height="18dp" android:layout_centerVertical="true" android:layout_marginRight="8dp" android:background="@drawable/circle_green" /> <TextView android:id="@+id/qlState" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未满" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <!-- 属性名 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SOC:" android:textStyle="bold" /> <TextView android:id="@+id/soc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginRight="8dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="%" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电压:" android:textStyle="bold" /> <TextView android:id="@+id/voltage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginRight="8dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="V" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电流:" android:textStyle="bold" /> <TextView android:id="@+id/electricity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginRight="8dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="车身状态:" android:textStyle="bold" /> <TextView android:id="@+id/carState" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正常" android:layout_marginRight="8dp" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="左电机转速:" android:textStyle="bold" /> <TextView android:id="@+id/lMotorSpeed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginRight="8dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="右电机转速:" android:textStyle="bold" /> <TextView android:id="@+id/rMotorSpeed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginRight="8dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="遥控器左电机速度:" android:textStyle="bold" /> <TextView android:id="@+id/lYkqMotorSpeed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginRight="8dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="遥控器右电机速度:" android:textStyle="bold" /> <TextView android:id="@+id/rYkqMotorSpeed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginRight="8dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout> <LinearLayout android:layout_height="wrap_content" android:layout_columnWeight="1" android:orientation="horizontal" android:layout_marginBottom="8dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="报文:" android:textStyle="bold" /> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginRight="8dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout> </GridLayout> </androidx.cardview.widget.CardView> </LinearLayout> 这是我的安卓xml代码,我现在发现左边一行的数据过宽会影响到右边的数据,怎么修改,才能让数据过宽的时候自动换行,不影响整体一边一半的布局
11-25
<com.mediatek.keyguard.Telephony.KeyguardSimPinPukMeView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:androidprv="http://schemas.android.com/apk/res-auto" android:id="@+id/keyguard_sim_pin_puk_me_view" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" androidprv:layout_maxWidth="@dimen/keyguard_security_width" androidprv:layout_maxHeight="@dimen/mtk_keyguard_security_max_height" android:gravity="center_horizontal"> <LinearLayout android:id="@+id/sim_info_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:gravity="center_vertical"> <TextView android:id="@+id/slot_num_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:ellipsize="marquee" android:singleLine="true" android:textDirection = "locale" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="?attr/wallpaperTextColor" android:visibility="gone" /> <ImageView android:id="@+id/keyguard_sim" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_lockscreen_sim"/> </LinearLayout> <LinearLayout android:id="@+id/keyguard_bouncer_frame" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical" android:layout_weight="1" android:layoutDirection="ltr" > <RelativeLayout android:id="@+id/row0" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:paddingBottom="16dp" > <com.android.keyguard.PasswordTextView android:id="@+id/simPinPukMeEntry" android:layout_width="@dimen/keyguard_security_width" android:layout_height="match_parent" android:gravity="center" android:layout_centerHorizontal="true" android:layout_marginRight="72dp" androidprv:scaledTextSize="28" /> <com.android.keyguard.NumPadButton android:id="@+id/delete_button" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:clickable="true" android:paddingTop="8dip" android:paddingBottom="8dip" android:paddingRight="8dp" android:paddingLeft="24dp" android:contentDescription="@string/keyboardview_keycode_delete" android:layout_alignEnd="@+id/simPinPukMeEntry" android:layout_alignParentRight="true" style="@style/NumPadKey.Delete" /> <View android:id="@+id/divider" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="#28FFFFFF" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <com.android.keyguard.NumPadKey android:id="@+id/key1" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="1" /> <com.android.keyguard.NumPadKey android:id="@+id/key2" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="2" /> <com.android.keyguard.NumPadKey android:id="@+id/key3" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="3" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <com.android.keyguard.NumPadKey android:id="@+id/key4" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="4" /> <com.android.keyguard.NumPadKey android:id="@+id/key5" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="5" /> <com.android.keyguard.NumPadKey android:id="@+id/key6" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:layout_weight="1" > <com.android.keyguard.NumPadKey android:id="@+id/key7" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="7" /> <com.android.keyguard.NumPadKey android:id="@+id/key8" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="8" /> <com.android.keyguard.NumPadKey android:id="@+id/key9" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="9" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/key_dismiss" style="@style/DismissPadKey" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" android:visibility="invisible" android:text="DISMISS" /> <com.android.keyguard.NumPadKey android:id="@+id/key0" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginEnd="@dimen/mtk_num_pad_key_margin_end" androidprv:textView="@+id/simPinPukMeEntry" androidprv:digit="0" /> <com.android.keyguard.NumPadButton android:id="@+id/key_enter" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" style="@style/NumPadKey.Enter" android:contentDescription="@string/keyboardview_keycode_enter" /> </LinearLayout> </LinearLayout> <include layout="@layout/keyguard_eca" android:id="@+id/keyguard_selector_fade_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="bottom|center_horizontal" android:gravity="center_horizontal"/> </com.mediatek.keyguard.Telephony.KeyguardSimPinPukMeView> 该界面数字键显示不全,分析问题并修改布局
11-28
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/beijing1" tools:ignore="ExtraText"> <!-- 头像 --> <ImageView android:id="@+id/iv_avatar" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:src="@drawable/touxiang" android:scaleType="centerCrop" /> <EditText android:id="@+id/et_user" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/iv_avatar" android:layout_marginTop="100dp" android:layout_marginLeft="32dp" android:layout_marginRight="32dp" android:background="@android:color/white" android:padding="8dp" android:hint="QQ号/邮箱/手机号" android:inputType="textPersonName" /> <EditText android:id="@+id/et_password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/et_user" android:layout_marginTop="40dp" android:layout_marginLeft="32dp" android:layout_marginRight="32dp" android:background="@android:color/white" android:padding="8dp" android:hint="请输入密码" android:inputType="textPassword" /> <!-- 登录和注册按钮 --> <LinearLayout android:id="@+id/button_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/et_password" android:layout_margin="16dp" android:orientation="horizontal" android:weightSum="2" tools:ignore="UnknownId"> <Button android:id="@+id/btn_login" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginTop="30dp" android:layout_below="@+id/et_password" android:text="登录" /> <Button android:id="@+id/btn_register" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_below="@+id/et_password" android:layout_weight="1" android:layout_marginTop="30dp" android:text="注册" /> </LinearLayout> <TextView android:id="@+id/forgotPasswordText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_centerHorizontal="true" android:layout_marginTop="15dp" android:layout_below="@+id/button_container" android:text="忘记密码" android:textSize="20sp" android:textColor="@color/white" /> <!-- 同意 QQ 服务协议文本 --> <TextView android:id="@+id/tv_agreement" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:textColor="@color/white" android:textSize="20sp" android:text="同意 QQ 服务协议 " /> </RelativeLayout> 晚上上述代码,使点击登陆和注册时的按钮时,弹出此程序正在完善的普通对话框,用指定onClick方法实现,要详细步骤
09-23
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="@dimen/dp_10" android:layout_marginRight="@dimen/dp_10" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/home_round_corner_line" > <ImageView android:layout_width="150dp" android:layout_height="150dp" android:src="@mipmap/car1" android:layout_gravity="center" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2021" android:textSize="15sp" android:textColor="@color/black" android:layout_gravity="center" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2021" android:textSize="15sp" android:textColor="@color/black" android:layout_gravity="center" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="@dimen/dp_10" android:paddingRight="@dimen/dp_10" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="22.98万" android:textSize="15sp" android:textColor="@color/tab_select" android:layout_gravity="center" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0.8万公里" android:textSize="15sp" android:textColor="@color/black" android:layout_gravity="center" android:layout_alignParentRight="true" /> </RelativeLayout> </LinearLayout> </LinearLayout>使用RelativeLayout把文字往图片上面盖点
11-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值