如果我的按钮不移动,我就无法在按钮上方放置文本视图...我该如何解决这个问题?
被困在这个问题上 4 个小时了...这很伤心,因为我正在尝试在我的布局上添加一个文本视图...
我的布局中心有 4 个按钮。我只想在四个按钮上方添加一个文本视图,如下所示(TextView 应该位于红色的位置):
当我将文本视图放置在按钮上方时,XML 布局视图中的拖放功能将不起作用。它只是使按钮居中。我也尝试过做什么this https://stackoverflow.com/questions/7887815/android-how-do-i-position-views-in-an-offset-relative-to-the-center-top-bottom建议回答,但锚视图本身不会位于中心!
这是我的 XML:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.me.circleswithmap.MainActivity"
android:id="@+id/layout"
android:gravity="center">
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Blue"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/bluesquare" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Green"
android:layout_below="@+id/Blue"
android:layout_alignLeft="@+id/Blue"
android:layout_alignStart="@+id/Blue"
android:background="@drawable/greensquare" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Red"
android:layout_alignTop="@+id/Blue"
android:layout_toRightOf="@+id/Blue"
android:layout_toEndOf="@+id/Blue"
android:background="@drawable/redsquare" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Purple"
android:layout_alignBottom="@+id/Green"
android:layout_alignLeft="@+id/Red"
android:layout_alignStart="@+id/Red"
android:background="@drawable/purplesquare" />
NEED SOME TEXTVIEW ALSO! WHENEVER I ADD ONE, IT DISCENTERS THE REST OF THE BUTTONS!
</RelativeLayout>
我应该怎么做才能解决这个问题?为什么这么难?我不应该使用相对布局吗?我怎样才能阻止它们相互关联?
非常感谢,
空指针异常
<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"
tools:context="com.example.ruchir.circleswithmap.MainActivity"
android:id="@+id/layout">
<LinearLayout android:layout_width="wrap_content"
android:id="@+id/text_container"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_height="wrap_content">
<TextView android:layout_width="match_parent"
android:text="pruebaskjahlkdjahslk"
android:layout_height="match_parent"/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Blue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Green"
android:layout_below="@+id/Blue"
android:layout_alignLeft="@+id/Blue"
android:layout_alignStart="@+id/Blue"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Red"
android:layout_alignTop="@+id/Blue"
android:layout_toRightOf="@+id/Blue"
android:layout_toEndOf="@+id/Blue"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Purple"
android:layout_alignBottom="@+id/Green"
android:layout_alignLeft="@+id/Red"
android:layout_alignStart="@+id/Red"/>
</RelativeLayout>
</RelativeLayout>
横向
<?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"
tools:context="com.example.me.circleswithmap.MainActivity"
android:id="@+id/layout">
<LinearLayout android:layout_width="wrap_content"
android:id="@+id/text_container"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="match_parent"
android:text="20:00"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout"
android:layout_below="@id/text_container"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Blue"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Green"
android:layout_below="@+id/Blue"
android:layout_alignLeft="@+id/Blue"
android:layout_alignStart="@+id/Blue"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Red"
android:layout_alignTop="@+id/Blue"
android:layout_toRightOf="@+id/Blue"
android:layout_toEndOf="@+id/Blue"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/Purple"
android:layout_alignBottom="@+id/Green"
android:layout_alignLeft="@+id/Red"
android:layout_alignStart="@+id/Red"/>
</RelativeLayout>
</RelativeLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)