浣跨敤FlexboxLayoutManager灏哛ecyclerView椤圭洰灞呬腑
瑕佸皢RecyclerView椤圭洰灞呬腑锛屼綘鍙互浣跨敤FlexboxLayoutManager鏉ュ疄鐜般€備笅闈㈡槸涓€绉嶅彲鑳界殑鏂规硶锛?/p>
棣栧厛锛岀‘淇濅綘鐨勯」鐩腑宸茬粡寮曞叆浜咶lexboxLayoutManager搴撱€傚湪build.gradle鏂囦欢鐨刣ependencies涓坊鍔犱互涓嬩唬鐮侊細
implementation 'com.google.android:flexbox:2.0.1'
鐒跺悗锛屽湪浣犵殑Activity鎴朏ragment涓紝鎵惧埌瑕佷娇鐢‵lexboxLayoutManager鐨凴ecyclerView锛屽苟灏哃ayoutManager璁剧疆涓篎lexboxLayoutManager銆備緥濡傦細
RecyclerView recyclerView = findViewById(R.id.recyclerView);
FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
layoutManager.setJustifyContent(JustifyContent.CENTER); // 灏嗛」鐩眳涓?/span>
recyclerView.setLayoutManager(layoutManager);
娉ㄦ剰锛岃繖閲屼娇鐢ㄤ簡setJustifyContent(JustifyContent.CENTER)
鏉ュ皢椤圭洰灞呬腑銆備綘涔熷彲浠ヤ娇鐢ㄥ叾浠栫殑JustifyContent
鍊兼潵瀹炵幇涓嶅悓鐨勫榻愭柟寮忋€?/p>
鏈€鍚庯紝鍦ㄤ綘鐨凴ecyclerView鐨凙dapter涓紝纭繚浣犵殑姣忎釜椤圭洰鐨勫竷灞€涓寘鍚?code>android:layout_width="wrap_content"锛岃繖鏍锋墠鑳戒娇椤圭洰鍦ㄦ按骞虫柟鍚戜笂灞呬腑鏄剧ず銆?/p>
杩欐牱锛屼綘鐨凴ecyclerView椤圭洰灏变細琚眳涓樉绀轰簡銆?/p>
相关问答