【发布时间】:2016-03-21 02:18:36
【问题描述】:
我有一个布局,中间有 4 个按钮。我只是想在四个按钮旁边添加一个 textview(不从中心移动四个按钮),如图所示(TextView 应该在红色的位置):
[![在此处输入图片描述][1]][1]
现在,我有自己的线性布局中的文本视图和自己的相对布局中的四个按钮,都在一个大的相对布局中。每当我尝试将文本视图移动到按钮的左侧时,按钮都会向右移动。我如何实现我所描绘的?这是我的 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.ruchir.circleswithmap.MainActivity"
android:id="@+id/layout"
android:gravity="center"
android:background="#010101">
<LinearLayout
android:id="@+id/text_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="20:00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:textColor="#fbfafa"
android:id="@+id/timetext" />
</LinearLayout>
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/Blue"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/bluesquare" />
<ImageButton
android:layout_width="150dp"
android:layout_height="150dp"
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="150dp"
android:layout_height="150dp"
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="150dp"
android:layout_height="150dp"
android:id="@+id/Purple"
android:layout_alignBottom="@+id/Green"
android:layout_alignLeft="@+id/Red"
android:layout_alignStart="@+id/Red"
android:background="@drawable/purplesquare" />
</RelativeLayout>
</RelativeLayout>
【问题讨论】:
-
@PiyushGupta 我已经解释了为什么不重复
-
你的
TextView真的需要它的尺寸match_parent吗?您是否尝试将其设置为wrap_content?另外,为什么你的TextView需要包裹在LinearLayout中?您是否在尝试移动layout_above属性时将其删除? -
@Amy 不,我不需要它是
match_parent,但只要把它放在它占用LinearLayout 的空间。你建议我怎么做?非常感谢 -
但是你的
LinearLayout环绕在你的TextView周围,这告诉LinearLayout它想要尽可能大——我认为Android 会感到困惑。如果您不需要 LinearLayout,请将相关属性移动到您的TextView并将其width和height设置为wrap_content。 -
我有解决方案,但不能作为答案。!!
标签: java android xml android-activity android-studio