【问题标题】:Cannot show all the content using wrap_content in Android无法在 Android 中使用 wrap_content 显示所有内容
【发布时间】:2017-07-18 09:10:34
【问题描述】:

谁能向我解释为什么我不能使用以下 XML 代码显示地址的内容。非常感谢你。内容应该多于3行,但我执行程序时只显示两行内容。

<?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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#e6e6e6"
android:orientation="vertical"
android:padding="1dp">

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



        <TextView

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#868585"
            android:padding="0dp"
            android:text="Basic Information"
            android:textColor="#ffffff" />

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

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

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="9"
                    app:srcCompat="@drawable/navigation" />

                <TextView
                    android:id="@+id/textView7"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="Address" />
            </LinearLayout>


            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@android:color/darker_gray" />


        </LinearLayout>


    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 首先您的滚动视图应该只有 1 个子视图。其次,您的地址文本视图的高度为 match_parent 而不是 wrap_content

标签: android xml android-layout layout android-wrap-content


【解决方案1】:

由于滚动视图,您的所有控件不可见,因为滚动视图可以直接处理一个孩子尝试使您的布局像这样

    <ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

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


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#868585"
        android:padding="0dp"
        android:text="Basic Information"
        android:textColor="#ffffff" />

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

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="9"
                app:srcCompat="@drawable/navigation" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Address" />
        </LinearLayout>


        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray" />


    </LinearLayout>

</LinearLayout>

</ScrollView>

【讨论】:

    【解决方案2】:

    试试这个

     <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#e6e6e6"
            android:orientation="vertical"
            android:padding="1dp">
    
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="1"
                        android:background="#868585"
                        android:padding="0dp"
                        android:text="Your text here"
                        android:textColor="#ffffff" />
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="9"/>
    
                        <TextView
                            android:id="@+id/textView7"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:text="Address" />
                    </LinearLayout>
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@android:color/darker_gray" />
                </LinearLayout>
            </ScrollView>
        </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      试试这个。滚动视图只处理一个直接类,然后给 imageview 高度 matchparent

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#e6e6e6"
          android:orientation="vertical"
          android:padding="1dp">
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="match_parent">
      
      <LinearLayout
          android:orientation="vertical"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content">
          <TextView
      
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:background="#868585"
              android:padding="0dp"
              android:text="Basic Information"
              android:textColor="#ffffff" />
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
      
              <LinearLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
      
                  <ImageView
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_weight="9"
                      app:srcCompat="@mipmap/ic_launcher" />
      
                  <TextView
                      android:id="@+id/textView7"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_weight="1"
                      android:text="Addhhhhhhhhhhhhhhhhhhvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhress" />
              </LinearLayout>
      
      
              <View
                  android:layout_width="match_parent"
                  android:layout_height="1dp"
                  android:background="@android:color/darker_gray" />
      
      
          </LinearLayout>
      </LinearLayout>
      </ScrollView>
      
      </LinearLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多