ScrollView 内的卡片
Cards Inside ScrollView
我在图像 1 中显示了这些 cardView 内部的 ScrollView(https://imgur.com/a/aO5Ss ) where the device is vertical and everything is as it's suppose to be but when the device is tilted Horizontally the 2nd row in the 4th row get's cutoff as shown here image-2(https://imgur.com/9ykZew4)。请帮助解释为什么会发生这种情况以及如何解决。 (注意 - 在第 4 张卡片中有一个列表视图,它由来自 colmn_row.xml 的文本视图膨胀)
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>
发生这种情况是因为您的 ScrollView 中有一个 LinearLayout:
尝试将 wrap_content 设置为 layout_width 和 layout_height 属性。
此问题是由于 layout_weight 中的常量值 colmn_row.xml,
colmn_row.xml 将根据设备屏幕宽度呈现。所有四个项目将共享相等的 width.Make 您的列 header 也是设备宽度特定的。
下面是第四张卡片布局的代码,
<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 中的布局扩展为 parent 或在 NestedScrollView 中使用 reyclerview,我已将 lisview 更新为 linearlayout
下面是动态创建视图的代码
@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);
}
我在图像 1 中显示了这些 cardView 内部的 ScrollView(https://imgur.com/a/aO5Ss ) where the device is vertical and everything is as it's suppose to be but when the device is tilted Horizontally the 2nd row in the 4th row get's cutoff as shown here image-2(https://imgur.com/9ykZew4)。请帮助解释为什么会发生这种情况以及如何解决。 (注意 - 在第 4 张卡片中有一个列表视图,它由来自 colmn_row.xml 的文本视图膨胀)
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>
发生这种情况是因为您的 ScrollView 中有一个 LinearLayout:
尝试将 wrap_content 设置为 layout_width 和 layout_height 属性。
此问题是由于 layout_weight 中的常量值 colmn_row.xml, colmn_row.xml 将根据设备屏幕宽度呈现。所有四个项目将共享相等的 width.Make 您的列 header 也是设备宽度特定的。
下面是第四张卡片布局的代码,
<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 中的布局扩展为 parent 或在 NestedScrollView 中使用 reyclerview,我已将 lisview 更新为 linearlayout
下面是动态创建视图的代码
@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);
}