Android 在Recyclerview中居中CardView
这是我的密码: 主要活动:Android 在Recyclerview中居中CardView,android,Android,这是我的密码: 主要活动: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } 他的布局: <
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
他的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context="com.example.programista.dobra_robimy.MainActivity"
android:orientation="vertical">
<fragment
android:id="@+id/list_frag"
class="com.example.programista.dobra_robimy.FragmentListAlarms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/alarms_recycler"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scrollbars="vertical" />
碎片列表报警:
public class FragmentListAlarms extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RecyclerView alarmsRecycler = (RecyclerView) inflater.inflate(R.layout.fragment_list_alarms, container, false);
String[] alarmsNames = new String[Alarms.AlarmsList.length];
for (int i = 0; i < alarmsNames.length; i++) {
alarmsNames[i] = Alarms.AlarmsList[i].getName();
}
TextAdapter adapter = new TextAdapter(alarmsNames);
alarmsRecycler.setAdapter(adapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
alarmsRecycler.setLayoutManager(layoutManager);
return alarmsRecycler;
}
}
公共类FragmentListAlarms扩展片段{
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
RecyclerView alarmsRecycler=(RecyclerView)充气器。充气(R.layout.fragment\u list\u报警,容器,错误);
String[]alarmsNames=新字符串[Alarms.AlarmsList.length];
对于(int i=0;i
他的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
tools:context="com.example.programista.dobra_robimy.MainActivity"
android:orientation="vertical">
<fragment
android:id="@+id/list_frag"
class="com.example.programista.dobra_robimy.FragmentListAlarms"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/alarms_recycler"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:scrollbars="vertical" />
我在每个可能的地方都设置了gravity=“center”,但CardView仍然粘贴在左边距上:(
编辑
正如我所知,RecyclerView的项目不能简单地通过为父视图设置layout_gravity=“center”来居中,但还有另一种方法:
<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="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="200dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView/>
</android.support.v7.widget.CardView>
</LinearLayout>
好的,我明白了,但我不能把它放在某个中心吗?您可以为您的CardView设置父布局(例如,LinearLayout),然后将CardView放在中心。检查我编辑的答案。
<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="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="200dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<TextView/>
</android.support.v7.widget.CardView>
</LinearLayout>