Android 是否将文本视图添加到布局?
如果我的按钮没有移动,我无法将文本视图放在按钮上方……我如何解决此问题? 在这个问题上被困了4个小时…这很令人伤心,因为我正试图在我的版面上添加一个文本视图 我有一个布局,中间有4个按钮。我只想在四个按钮上方添加一个textview,如图所示(textview应位于红色的位置): 当我将textview放置在按钮上方时,XML布局视图中的拖放将不起作用。它只是使按钮偏离中心。我也试着按照答案的建议做,但是锚定视图本身不会被定位在中心强> 以下是我的XML:Android 是否将文本视图添加到布局?,android,xml,android-layout,android-activity,android-studio,Android,Xml,Android Layout,Android Activity,Android Studio,如果我的按钮没有移动,我无法将文本视图放在按钮上方……我如何解决此问题? 在这个问题上被困了4个小时…这很令人伤心,因为我正试图在我的版面上添加一个文本视图 我有一个布局,中间有4个按钮。我只想在四个按钮上方添加一个textview,如图所示(textview应位于红色的位置): 当我将textview放置在按钮上方时,XML布局视图中的拖放将不起作用。它只是使按钮偏离中心。我也试着按照答案的建议做,但是锚定视图本身不会被定位在中心 以下是我的XML: <?xml version="1.
<?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>
还需要一些文本视图!每当我添加一个按钮时,它就会显示其余的按钮!
我应该怎么做来解决这个问题?为什么这么难?我应该不使用相对布局吗?我怎样才能阻止它彼此相对
非常感谢
NullPointerException一个简单而快速的解决方案可以是使用两种布局:保持
相对布局
并将其嵌入垂直线性布局
。然后,您只需将文本视图
添加到线性布局
,然后再添加到相对视图
注意:使用多层布局可能会影响性能,因此不要过度使用。不过,两个布局应该不会有什么坏处。您可以将按钮包装在布局中,然后在其上方放置文本视图,并告诉新布局位于文本视图下方
<?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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
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">
<TextView
android:id="@+id/textview111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password_text_view"
android:layout_alignParentTop="true"/>
<RelativeLayout
android:layout_below="@id/textview111"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/Blue"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/primaryColor" />
<ImageButton
android:id="@+id/Green"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignLeft="@+id/Blue"
android:layout_alignStart="@+id/Blue"
android:layout_below="@+id/Blue"
android:background="@color/primaryColor" />
<ImageButton
android:id="@+id/Red"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignTop="@+id/Blue"
android:layout_toEndOf="@+id/Blue"
android:layout_toRightOf="@+id/Blue"
android:background="@color/primaryColor" />
<ImageButton
android:id="@+id/Purple"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBottom="@+id/Green"
android:layout_alignLeft="@+id/Red"
android:layout_alignStart="@+id/Red"
android:background="@color/primaryColor" />
</RelativeLayout>
</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>
它适用于所有大小的设备,但您必须动态地选择左侧
文本视图的文本大小<代码>文本视图
根据设备密度,文本大小应较小。希望这会有帮助
<?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:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#010101"
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" >
<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:id="@+id/timetext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="20:00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fbfafa"
android:textSize="50sp" />
</LinearLayout>
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="50:00"
android:gravity="center_vertical|right"
android:layout_toLeftOf="@+id/relativeLayout"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fbfafa"
android:textSize="50sp" />
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageButton
android:id="@+id/Blue"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/Green"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/Blue"
android:background="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/Red"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignTop="@+id/Blue"
android:layout_toEndOf="@+id/Blue"
android:layout_toRightOf="@+id/Blue"
android:background="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/Purple"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBottom="@+id/Green"
android:layout_alignLeft="@+id/Red"
android:layout_alignStart="@+id/Red"
android:background="@drawable/ic_launcher" />
</RelativeLayout>
您可以将FrameLayout作为最外层的布局元素,并将RelativeLayout和TextView放在该元素内。@1615903如何将当前代码移动到新布局中?请随时发布答案。感谢您的回复:-)如何将当前布局移动到LinearLayout?我只会在XML文件中手动执行,我不太使用wysiwyg设计器。最简单的方法可能是将您现在拥有的标记类型从更改为,然后在设计器中拖放一个新的RelativeLayout。不要忘记相应地更改布局属性(对于两种布局)!编辑:哦,当然,别忘了将按钮移动到新的RelativeLayout;)此外,我如何获得四个按钮中心在屏幕中部?我在相对布局中尝试了安卓:gravity=“center”,但它没有复制我的答案“原样”。他们在中间,好了,开始工作了。因此,没有背景,按钮之间有一个边距…在我添加背景后,边距消失了。有没有办法保持这一差距?如果没有背景绘图,很难回答这个问题。试着给每个图片按钮加上一些空白,所以我会给你们最好的答案打分,一旦我得到这个,我会喜欢你们的帖子。当我具有横向方向时,如何将四个按钮实际放置在中间。同样,布局没有被拖动。