简体   繁体   English

textview下面的textview linearlayout

[英]textview below textview linearlayout

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. 我正在使用LinearLayout,因为这是我可以使用layout_weight的唯一方法,而且我不熟悉在RelativeLayout中均匀对齐textviews。 (I'm new to android). (我是android新手)。 I'm making a calendar app and can't seem to figure out how to get a textview below my 7 textviews. 我正在创建一个日历应用程序,似乎无法弄清楚如何在我的7个文本视图下面获取textview。 (The 7 textviews are the days of the week). (7个textviews是一周中的几天)。 They end up showing all on the same row. 他们最终在同一行显示所有内容。

How do I get the final textview below my 7 days? 如何获得7天以下的最终文本视图? Please note I've searched this question on stackoverflow but I haven't found anything that made sense to me or worked. 请注意我在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>

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>

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>

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. 要将摘要放置在其余视图下方,您可以将主线性布局的方向更改为垂直,并将7个textView与另一个水平方向的线性布局封装在一起。 Here is a code: 这是一个代码:

<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>

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天封装在额外的线性布局和RootLayout中的最后一个TextView中。

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标记设置为“Vertical”

An even simpler explanation: You are using a LinearLayout with orientation set to horizontal; 更简单的解释:您正在使用方向设置为水平的LinearLayout; this is telling Android "All of these items should be placed next to each other in a single row". 这告诉Android“所有这些项目应该在一行中彼此相邻”。 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). 然后,您的最终textView可以是垂直布局的子项而不是水平布局(这是其他答案显示的内容)。

From what i understand, you want to do is this: 据我所知,你想做的是:

  • TextView1 TextView1
  • TextView2 TextView2
  • (Some more TextViews) (还有一些TextViews)
  • TextViewLast TextViewLast

To achieve this in the VERTICAL ORIENTATION, i did this in my .xml layout file. 为了在VERTICAL ORIENTATION中实现这一点,我在我的.xml布局文件中做到了这一点。 Hope it works for you too. 希望它也适合你。

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"

And the result is this: 结果如下:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM