Android 文本视图下面的文本视图线性布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12232144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:06:24  来源:igfitidea点击:

textview below textview linearlayout

android

提问by Terence Chow

I'm using LinearLayout because it's the only way I can use layout_weight and I'm not familiar enough with aligning textviews evenly in RelativeLayout. (I'm new to android). I'm making a calendar app and can't seem to figure out how to get a textview below my 7 textviews. (The 7 textviews are the days of the week). They end up showing all on the same row.

我使用 LinearLayout 是因为它是我可以使用 layout_weight 的唯一方法,而且我不太熟悉在 RelativeLayout 中均匀对齐文本视图。(我是安卓新手)。我正在制作一个日历应用程序,但似乎无法弄清楚如何在我的 7 个文本视图下方获得文本视图。(7 个文本视图是一周中的几天)。它们最终显示在同一行。

How do I get the final textview below my 7 days? Please note I've searched this question on stackoverflow but I haven't found anything that made sense to me or worked.

如何获得低于我的 7 天的最终文本视图?请注意,我在 stackoverflow 上搜索了这个问题,但没有找到任何对我有意义或有用的东西。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="top" >

<TextView
    android:id="@+id/day1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="@string/day1"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day2"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day3"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day4"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day5"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/day6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day6"
    android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
    android:id="@+id/day7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/day7"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="a text string below" 
    android:textAppearance="?android:attr/textAppearanceSmall" />


</LinearLayout>

回答by Yellos

Simplified:

简化:

<LinearLayout android:orientation="vertical">    
    <LinearLayout android:orientation="horizontal" android:layout_weight="1">
        <!-- TextView 1,2,3,4,5,6,7 here -->
    </LinearLayout>
    <TextView android:layout_weight="1"/><!-- (TextView 8) -->
</LinearLayout>

回答by yildirimyigit

If you use RelativeLayout, you will be able to use *android:layout_below* attribute.

如果您使用 RelativeLayout,您将能够使用 *android:layout_below* 属性。

All of the textviews you use have the same weight. As far as I know, you may just delete those lines.

您使用的所有文本视图都具有相同的权重。据我所知,您可以删除这些行。

If I were you, I'd consider a hierarchy as follows:

如果我是你,我会考虑如下层次结构:

<RelativeLayout>
    <LinearLayout android:id="@+id/blabla">
        *7 textviews*
    </LinearLayout>
    <TextView>
        android:layout_below= @+id/blabla
    </TextView>
</RelativeLayout>

回答by Marcin S.

To place a view with the summary below the rest of the views you can change the orientation of the main Linear Layout to vertical and enclose your 7 textViews with another Linear Layout with horizontal orientation. Here is a code:

要在其余视图下方放置带有摘要的视图,您可以将主线性布局的方向更改为垂直,并将您的 7 个 textView 与另一个具有水平方向的线性布局包围起来。这是一个代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="top" >

<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/day1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/day1"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day2"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day3"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day4"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day5"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day6"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="@+id/day7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/day7"
android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="a text string below" 
android:textAppearance="?android:attr/textAppearanceSmall" />


</LinearLayout>

回答by mudrak patel

From what i understand, you want to do is this:

据我了解,您想要做的是:

  • TextView1
  • TextView2
  • (Some more TextViews)
  • TextViewLast
  • 文本视图1
  • 文本视图2
  • (更多的 TextViews)
  • 文本视图最后一个

To achieve this in the VERTICAL ORIENTATION, i did this in my .xml layout file. Hope it works for you too.

为了在垂直方向中实现这一点,我在我的 .xml 布局文件中做到了这一点。希望它也适用于你。

NOTE: You DO NOT need nested Linear Layouts.This can be achieved by one Linear Layout.

注意:您不需要嵌套的线性布局。这可以通过一个线性布局来实现。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fullNameTextView"
        android:text="@string/fullNameString"
        android:textSize="@dimen/mainActivityFontSize"
        android:layout_marginLeft="@dimen/mainActivityMarginLeft"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/programNameTextView"
        android:textSize="@dimen/mainActivityFontSize"
        android:layout_marginLeft="@dimen/mainActivityMarginLeft"
        android:text="@string/programName"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/semesterTextView"
        android:textSize="@dimen/mainActivityFontSize"
        android:layout_marginLeft="@dimen/mainActivityMarginLeft"
        android:text="@string/semesterTextView"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/courseNameTextView"
        android:textSize="@dimen/mainActivityFontSize"
        android:layout_marginLeft="@dimen/mainActivityMarginLeft"
        android:text="@string/courseName"/>

</LinearLayout>

Three things that made my layout the way I want (and hope you also want the same type of layout):

使我的布局成为我想要的方式的三件事(希望你也想要相同类型的布局):

  1. android:layout_width="match_parent"
  2. android:layout_height="wrap_content"
  3. android:orientation="vertical"
  1. android:layout_width="match_parent"
  2. android:layout_height="wrap_content"
  3. android:orientation="vertical"

And the result is this:

结果是这样的:

回答by Chris

Don′t know if I unserstand it right, but what you can try ist to encapsulate the days 1-7 in an extra Linear Layout and the last TextView in the RootLayout.

不知道我理解对不对,但是你可以尝试将第 1-7 天封装在一个额外的 Linear Layout 中,并将最后一个 TextView 封装在 RootLayout 中。

For Example:

例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="top" >


<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="top" >

  <!-- TextView for Days 1 to 7 in here -->

</LinearLayout>

<!-- Last TextView in here -->

</LinearLayout>

Edit: And try to set the android:orientation Tag to "Vertical"

编辑:并尝试将 android:orientation 标签设置为“垂直”

回答by Scott

An even simpler explanation: You are using a LinearLayout with orientation set to horizontal; this is telling Android "All of these items should be placed next to each other in a single row". What you want is a vertical linear layout which contains the linear layout you currently have. Your final textView can then be a child of the vertical layout rather than the horizontal one (which is what the other answers are showing).

一个更简单的解释:您使用的 LinearLayout 方向设置为水平;这告诉Android“所有这些项目都应该放在一行中”。您想要的是垂直线性布局,其中包含您当前拥有的线性布局。然后,您的最终 textView 可以是垂直布局的子项,而不是水平布局的子项(这是其他答案所显示的)。