关于ConstraintLayout布局
我想问下对于最新的ConstraintLayout布局,应该如何在它内部使用ScrollView呢?我在网上查了很多方法都有问题。
正在回答 回答被采纳积分+1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <? xml version = "1.0" encoding = "utf-8" ?> < android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent" > < ScrollView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:orientation = "vertical" tools:layout_editor_absoluteY = "8dp" tools:layout_editor_absoluteX = "8dp" > < GridView android:id = "@+id/gridview" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:columnWidth = "90dp" android:numColumns = "3" android:verticalSpacing = "10dp" android:horizontalSpacing = "10dp" android:stretchMode = "spacingWidth" android:gravity = "center" /> </ ScrollView > </ android.support.constraint.ConstraintLayout > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class ExampleActivity extends Activity { private GridView gridView; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_example1); gridView= (GridView) findViewById(R.id.gridview); List<String> strList= new ArrayList<String>(); for ( int i= 0 ;i< 3 ;i++) { strList.add( "慕课网" +i); } ArrayAdapter<String>arrayAdapter= new ArrayAdapter<String>( this ,R.layout.item_gridview1,strList); gridView.setAdapter(arrayAdapter); gridView.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(ExampleActivity. this ,position+ "" ,Toast.LENGTH_SHORT).show(); } } ); } } |
上面是这个界面的布局和java代码,下面是运行的效果。内部控件用的是GridView。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--布局实现-->
</ScrollView>
</android.support.constraint.ConstraintLayout>
如果像这样使用ScrollView,内部控件就会失效。如果把ScrollView套在ConstraintLayout外部,ConstraintLayout就会失效。所以应该如何摆放这个ScrollView呢
- 参与学习 504 人
- 提交作业 1003 份
- 解答问题 1044 个
本专题是专为有Java语言基础想入坑Android开发的同学们准备的。从AndroidUI基础一直到网络,数据存储,一步到位入门Android开发。如果你会Java,那就不要犹豫了~ ~
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧