2017-09-22 128 views
1

我有这些cardView里面的图片-1(https://imgur.com/a/aO5Ss)中显示的ScrollView,其中设备是垂直的,一切都如设想的那样,但是当设备水平倾斜时,第四行中的第二行get cutoff如图所示image-2(https://imgur.com/9ykZew4)。请帮助解释为什么会发生这种情况,以及如何解决这个问题。 (注意 - 在第四卡有其通过从colmn_row.xml TextView的膨胀的列表视图)ScrollView里面的卡片

activity_main.xml中

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="16dp" 
android:background="#ddd" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:showIn="@layout/activity_main" 
tools:context=".MainActivity"> 

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

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




<android.support.v7.widget.CardView 
    android:id="@+id/num_stu" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#fff" 
     android:elevation="10dp" 
     android:padding="16dp"> 

     <ImageView 
      android:id="@+id/cardview_image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="18dp" 
      android:src="@mipmap/ic_launcher_round" /> 

     <TextView 
      android:id="@+id/cardview_list_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/cardview_image" 
      android:paddingBottom="5dp" 
      android:text="Students" 
      android:textSize="24sp" /> 

     <TextView 
      android:id="@+id/num_boys" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/cardview_list_title" 
      android:layout_toRightOf="@+id/cardview_image" 
      android:textColor="#8b8b8b" /> 
     <TextView 
      android:id="@+id/num_girls" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/cardview_list_title" 
      android:layout_toRightOf="@+id/num_boys" 
      android:textColor="#8b8b8b" /> 
     <TextView 
      android:id="@+id/total_stu" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/num_girls" 
      android:layout_toRightOf="@+id/cardview_image" 
      android:textColor="#8b8b8b" /> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

<android.support.v7.widget.CardView 
    android:id="@+id/num_teachers" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/num_stu" 
    android:layout_marginTop="16dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#fff" 
     android:elevation="8dp" 
     android:padding="16dp"> 

     <ImageView 
      android:id="@+id/cardview_image1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="18dp" 
      android:src="@mipmap/ic_launcher_round" /> 

     <TextView 
      android:id="@+id/cardview_list_title1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/cardview_image1" 
      android:paddingBottom="5dp" 
      android:text="Staff" 
      android:textSize="24sp" /> 

     <TextView 
      android:id="@+id/numTeaching" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/cardview_list_title1" 
      android:layout_toRightOf="@+id/cardview_image1" 
      android:textColor="#8b8b8b" /> 
     <TextView 
      android:id="@+id/non_num_Teaching" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/numTeaching" 
      android:layout_toRightOf="@+id/cardview_image1" 
      android:textColor="#8b8b8b" /> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 

<android.support.v7.widget.CardView 
    android:id="@+id/attendance" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="16dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#fff" 
     android:elevation="5dp" 
     android:padding="16dp"> 

     <ImageView 
      android:id="@+id/cardview_image2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_marginRight="18dp" 
      android:src="@mipmap/ic_launcher_round" /> 

     <TextView 
      android:id="@+id/cardview_list_title2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_toRightOf="@+id/cardview_image2" 
      android:paddingBottom="5dp" 
      android:text="Attendance" 
      android:textSize="24sp" /> 


     <TextView 
      android:id="@+id/stuatten" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/cardview_list_title2" 
      android:layout_toRightOf="@+id/cardview_image2" 
      android:textColor="#8b8b8b"/> 
     <TextView 
      android:id="@+id/staffatten" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/stuatten" 
      android:layout_toRightOf="@+id/cardview_image2" 
      android:textColor="#8b8b8b" /> 


    </RelativeLayout> 

</android.support.v7.widget.CardView> 

<!-- Fourth Card --> 

<android.support.v7.widget.CardView 
    android:id="@+id/notice" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="16dp"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#fff" 
     android:elevation="8dp" 
     android:padding="16dp"> 



     <TextView 
      android:id="@+id/title" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_alignParentTop="true" 
      android:paddingBottom="5dp" 
      android:textSize="19sp" 
      android:textStyle="bold" 
      android:text="ID"/> 


     <TextView 
      android:id="@+id/title1" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_alignParentTop="true" 
      android:paddingBottom="5dp" 
      android:textSize="19sp" 
      android:textStyle="bold" 
      android:layout_marginLeft="115dp" 
      android:layout_toRightOf="@+id/title" 
      android:text="Title"/> 

     <TextView 
      android:id="@+id/title2" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_alignParentTop="true" 
      android:paddingBottom="5dp" 
      android:textSize="19sp" 
      android:textStyle="bold" 
      android:layout_toRightOf="@+id/title1" 
      android:layout_marginLeft="90dp" 
      android:text="Date From"/> 

     <TextView 
      android:id="@+id/title3" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="60dp" 
      android:paddingBottom="5dp" 
      android:textSize="19sp" 
      android:textStyle="bold" 
      android:text="Date To"/> 


     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/title" > 
     </ListView> 



</RelativeLayout> 
</android.support.v7.widget.CardView> 

</LinearLayout> 

</ScrollView> 
</LinearLayout> 

colmn_row.xml -

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


<TextView 
    android:id="@+id/name" 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:textStyle="bold" 
    android:textSize="17sp"/> 

<TextView 
    android:id="@+id/gender" 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:textSize="17sp"/> 

<TextView 
    android:id="@+id/age" 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:textSize="17sp"/> 

<TextView 
    android:id="@+id/status" 
    android:layout_height="wrap_content" 
    android:layout_width="0dp" 
    android:layout_alignParentLeft="true" 
    android:layout_weight="1" 
    android:textSize="17sp"/> 

</LinearLayout> 
+0

有你声明布局土地文件夹中的布局? – Vij

+0

@Vij我不熟悉前端开发,能否请您解释一下布局土地是什么以及它的用途。谢谢 –

+0

复制布局文件在layout-land文件夹中,然后你可以看到这个预览,你可以参考这个https://developer.android.com/guide/practices/screens_support.html – Vij

回答

0

此问题是由于在colmn_row.xmlcolmn_row.xml将根据设备屏幕宽度呈现恒定layout_weight值。并且所有四个项目将共享相同的宽度。制作您的列标题也是一个特定的设备宽度。

下面是4卡布局的代码,

<android.support.v7.widget.CardView 
android:id="@+id/notice" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="16dp"> 

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#fff" 
    android:elevation="8dp" 
    > 



    <TextView 
     android:id="@+id/title" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:paddingBottom="5dp" 
     android:textSize="19sp" 
     android:textStyle="bold" 
     android:text="ID"/> 


    <TextView 
     android:id="@+id/title1" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:paddingBottom="5dp" 
     android:textSize="19sp" 
     android:textStyle="bold" 
     android:layout_weight="1" 
     android:text="Title"/> 

    <TextView 
     android:id="@+id/title2" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:paddingBottom="5dp" 
     android:textSize="19sp" 
     android:textStyle="bold" 
     android:text="Date From"/> 

    <TextView 
     android:id="@+id/title3" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:paddingBottom="5dp" 
     android:textSize="19sp" 
     android:textStyle="bold" 
     android:text="Date To"/> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/dynamic_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
    </LinearLayout> 
</LinearLayout> 
</android.support.v7.widget.CardView> 

编辑:这是一个糟糕的主意,把列表视图内滚动视图,尝试充气内的LinearLayout布局,家长或使用内部NestedScrollView reyclerview,我已经更新lisview到的LinearLayout

下面是创建viiew代码动态

@Override 
protected void onCreate(Bundle savedInstanceState) { 

//.... 
linearLayout = (LinearLayout) findViewById(R.id.dynamic_layout); 

    String[] demo = new String[]{"Hello","World","Java","Android"}; 
    linearLayout.removeAllViews(); 
    for(int i=0;i<demo.length;i++){ 
     addView(demo[i]); 
    } 
} 

//Method to create view dynamically 
private void addView(String data){ 
    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    //You can use this view to findViewById, setOnClickListener, setText etc; 
    View view = layoutInflater.inflate(R.layout.column_row,null,false); 

    //FindView using inflated view 
    TextView textView = view.findViewById(R.id.name); 
    linearLayout.addView(view); 

} 
+0

感谢您的答复,但没有工作出来,它也使行可滚动像这样 - https://imgur.com/a/vcwFa –

+0

@AsfanUlla对不起,它的列表视图也有列标题文本家长布局,请参阅最新的答案。它现在会工作。 –

+0

如果我困扰你,我很抱歉。你的代码确实有助于对齐textview的内容,但当手机倾斜到横向时,第二行仍然不出现。在这个图像中,我已经滚动下来,但第二行似乎并没有出现 - https://imgur.com/vhJonvT。我想这与卡本身有关。 –

0

发生这种情况因为你有你的滚动型内的LinearLayout:

enter image description here

尝试将wrap_content设置为layout_width和layout_height属性。

+0

谢谢为回复,但当我删除,我得到一个新的错误 - java.lang.IllegalStateException:ScrollView可以主持只有一个直接的孩子 –

+0

对不起,也没有工作。 –