【问题标题】:Set a LinearLayout to scroll设置 LinearLayout 以滚动
【发布时间】:2013-08-09 15:57:22
【问题描述】:

我在 stackoverflow 中阅读了很多关于制作线性布局滚动的帖子,并应用了所有具体建议以使其工作,但它仍然没有显示在左侧。我是 android 新手,不知道我做错了什么。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1">
<LinearLayout
    android:id="@+id/expense"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:gravity="start"
    android:orientation="vertical"
    tools:context=".Expense" >

    <LinearLayout
        android:id="@+id/tedsts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/rounded"
        android:clickable="true"
        android:weightSum="1.0" >

        <TextView
            android:id="@+id/testffg"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight=".30"
            android:text="Matter"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/tedssss"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight=".65"
            android:ellipsize="end"
            android:paddingLeft="10dp"
            android:singleLine="true"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ImageView
            android:layout_width="15dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.05"
            android:src="@drawable/r_arrow" />
    </LinearLayout>

    <TextView
        android:id="@+id/tetxttx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="10dp"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:singleLine="true"  
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/gray_dark"
        android:visibility="invisible" />

    <EditText
        android:id="@+id/teetttsss"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/rounded_all"
        android:ems="10"
        android:gravity="top"
        android:hint="dgdfgfgf"
        android:inputType="textMultiLine" />

    <LinearLayout
        android:id="@+id/tesssstt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@drawable/rounded_all_clickable_selector"
        android:clickable="true"
        android:weightSum="1.0" >

        <TextView
            android:id="@+id/yttrree"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight=".20"
            android:text="Date"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/rrrefffe"
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight=".75"
            android:ellipsize="end"
            android:paddingLeft="10dp"
            android:singleLine="true"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <ImageView
            android:layout_width="15dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.05"
            android:src="@drawable/r_arrow" />
    </LinearLayout>

    <RadioButton
        android:id="@+id/dfgrrrrr"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="5dp"
        android:text="Cost"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/gergrerrr"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-4dp"
        android:background="@drawable/rounded_all_clickable"
        android:ems="10"
        android:hint=""
        android:inputType="numberDecimal"
        android:singleLine="true" >
    </EditText>

    <RadioButton
        android:id="@+id/rerreee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="5dp"
        android:text="Time"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/gggrrrrre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/roundede"
        android:ems="10"
        android:hint="grrrergg."
        android:inputType="numberDecimal"
        android:singleLine="true"
        android:visibility="gone" >
    </EditText>

    <EditText
        android:id="@+id/65gfhhggf"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-4dp"
        android:background="@drawable/rounded"
        android:ems="10"
        android:hint=""
        android:inputType="numberDecimal" >
    </EditText>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/gfhgfhtrhrth"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="15dp"
            android:visibility="gone" />

        <TextView
            android:id="@+id/ghjjhhhgh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@+id/expense_TV_total"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageButton
            android:id="@+id/hgjhhghjhg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/save_selector" />
    </RelativeLayout>
</LinearLayout>
</ScrollView>

【问题讨论】:

标签: android android-layout


【解决方案1】:

使用这个:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView ...>

<LinearLayout ...>

...
...

</LinearLayout>

</ScrollView>

并将 hieght 设置为

android:layout_height="wrap_content"

正如卡拉库里所说的

【讨论】:

    【解决方案2】:
    <?xml version="1.0" encoding="utf-8"?>
    
    <ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    
        //Your Main Layout
    
        <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical"     
         android:weightSum="100">
    
        // First Sub Layout Under Main Layout
           <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" 
            android:layout_weight="10"
            android:weightSum="100" >
    
               <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="TextView" 
                android:layout_weight="70" />
    
               <EditText
                android:id="@+id/editText1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="30" />
    
            </LinearLayout>// Finishing First Sub layout 
    
    // Second Sub Layout Under Main Layout
           <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" 
            android:layout_weight="10"
            android:weightSum="100" >
    
               <TextView
                android:id="@+id/textView2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="TextView" 
                android:layout_weight="70" />
    
               <EditText
                android:id="@+id/editText2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="30" />
    
            </LinearLayout>// Finishing Second Sub layout 
    
    similarly for 3rd,4rth,5th sub layouts and so on........
    
    </LinearLayout> // Finishing Main Layout
    </ScrollView>   // Finishing ScrollView 
    

    【讨论】:

      【解决方案3】:

      让 ScrollView 的孩子有android:layout_height="wrap_content"(目前你的有match_parent

      【讨论】:

      • 我运气不好,我将其修改为 wrap_content 但什么也没有
      • 您还没有为 ScrollView 设置 layout_width 和 layout_height。两者都应该是match_parent
      【解决方案4】:

      添加

      android:layout_width="match_parent"
      android:layout_height="match_parent"
      

      到你的根 ScrollView

      【讨论】:

        【解决方案5】:

        希望这段代码对你有所帮助。

            android:layout_width="match_parent"
            android:layout_height="match_parent">
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-02
          • 2018-11-02
          相关资源
          最近更新 更多