按钮位置因 texview 大小而改变

Button position changes because of texview size

我有一个 TextView,它会在点击按钮时显示不同的文本。但问题是,按钮位置因 TextView 尺寸而改变。我不想在不使用边距的情况下改变按钮位置。请帮忙。

我的XML设计代码。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.arnab.androiddevelopmentfundamentals.MainActivity">

    <TextView
        android:id="@+id/MyTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="@string/ui_and_events"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/MyText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/MyTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:text="@string/my_text"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/MyText"
        android:layout_below="@+id/MyText"
        android:layout_marginTop="33dp"
        android:text="@string/change_1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="48dp"
        android:text="@string/change_2" />

    </RelativeLayout>

我的 java 按钮点击代码。

package com.example.arnab.androiddevelopmentfundamentals;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView MyText;
    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyText = (TextView)findViewById(R.id.MyText);
        button1 = (Button)findViewById(R.id.button1);

        button1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == button1){
            MyText.setText("You have clicked button 1");
        }
    }
}

那么,如何在不使用保证金的情况下解决这个问题。

如您所见,按钮位置在 onclick 事件后发生变化。

按钮不要使用android:layout_alignEnd="@+id/MyText",按钮会移动到文本的末尾,如果文本长度改变尝试使用此代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.arnab.androiddevelopmentfundamentals.MainActivity">

    <TextView
        android:id="@+id/MyTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="@string/ui_and_events"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/MyText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/MyTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:text="@string/my_text"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:text="@string/change_1"
        android:layout_below="@+id/MyText"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="@string/change_2"
        android:layout_below="@+id/button1"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignStart="@+id/button1" />

</RelativeLayout>

删除

 android:layout_alignEnd="@+id/MyText"

从你的按钮一开始!它让您的 button1 附加到文本的 right/end。并且由于 button1 附加到按钮 2,因此当文本更改时它们都移动。从 button2 中删除上述 属性 后,您将看到两个按钮都将出现在位置 [=] 的 left/start 上15=]

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

    <TextView
        android:id="@+id/MyTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="zxczxc"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/MyText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/MyTitle"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:text="asdas"
        android:textColor="@color/colorPrimary"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/MyText"
        android:layout_marginTop="33dp"
        android:text="abc" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="48dp"
        android:text="qwe" />

如果您希望按钮始终居中,那么这是使用 layout_gravityLinearLayout 作为容器标签(根标签)的解决方案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:orientation="vertical"
   android:layout_height="match_parent">

   <TextView
       android:id="@+id/MyTitle"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="28dp"
       android:text="zxczxc"
       android:textSize="30sp" />

    <TextView
       android:id="@+id/MyText"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/MyTitle"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="39dp"
       android:text="asdas"
       android:textColor="@color/colorPrimary"
       android:textSize="30sp" />

    <Button
       android:id="@+id/button1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/MyText"
       android:layout_marginTop="33dp"
       android:layout_gravity="center"
       android:text="abc" />

    <Button
       android:id="@+id/button2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignStart="@+id/button1"
       android:layout_below="@+id/button1"
       android:layout_marginTop="48dp"
       android:layout_gravity="center"
       android:text="qwe" />

最简单的解决方案是为此目的使用 LinearLayout,因为您的要求是垂直方向的线性项目列表

<?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"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center_horizontal"
  tools:context="com.example.arnab.androiddevelopmentfundamentals.MainActivity">

  <TextView
    android:id="@+id/MyTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="28dp"
    android:text="@string/ui_and_events"
    android:textSize="30sp" />

  <TextView
    android:id="@+id/MyText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="39dp"
    android:text="You have clicked button 1"
    android:textColor="@color/colorPrimary"
    android:textSize="30sp" />

  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="33dp"
    android:text="@string/change_1"
     />

  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="48dp"
    android:text="@string/change_2"
    />

</LinearLayout>

这会将对象水平居中对齐,并完美地将它们一个接一个排列