以这种方式将布局居中的解决方案是什么?(android xml)
Which is a good solution to center the layouts in this way?(android xml)
我不想使用太多的线性布局来设置重心。有更好的解决方案吗?谢谢!
这是线性布局的解决方案,但有人告诉我我需要使用相对布局来实现,因为使用太多线性布局会导致渲染缓慢,但我没有成功使用相对布局。
<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:orientation="vertical"
tools:context="com.example.dan.movietracker.fragments.HomeFragment">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/action"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/advendure_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/adventure"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/animation_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/animation"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/comedy_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/comedy"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/crime_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/crime"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/documntary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/documentary"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/drama_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/drama"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/family_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/crime"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/fantasy_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/documentary"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/history_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/history"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/horror_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/horror"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/music_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/music"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/mystery_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/mystery"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/romance_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/romance"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/scienceFiction_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/science_fiction"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/thriller_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/thriller"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/war_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/war"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/western_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/western"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
一种方法是创建 item_layout.xml
并在下面添加:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/centerButton"
android:text="Mistery"/>
<Button
android:id="@+id/centerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Romance"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/centerButton"
android:text="Science Fiction"/>
</RelativeLayout>
然后每次添加内容时都可以将其用作行项目布局。希望对你有帮助
我不想使用太多的线性布局来设置重心。有更好的解决方案吗?谢谢!
这是线性布局的解决方案,但有人告诉我我需要使用相对布局来实现,因为使用太多线性布局会导致渲染缓慢,但我没有成功使用相对布局。
<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:orientation="vertical"
tools:context="com.example.dan.movietracker.fragments.HomeFragment">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/action_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/action"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/advendure_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/adventure"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/animation_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/animation"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/comedy_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/comedy"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/crime_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/crime"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/documntary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/documentary"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/drama_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/drama"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/family_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/crime"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/fantasy_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/documentary"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/history_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/history"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/horror_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/horror"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/music_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/music"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/mystery_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/mystery"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/romance_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/romance"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/scienceFiction_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/science_fiction"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="@dimen/margin_top_genres"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/thriller_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:layout_marginRight="@dimen/margin_right_genres"
android:text="@string/thriller"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/war_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/war"
android:layout_marginRight="@dimen/margin_right_genres"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
<TextView
android:id="@+id/western_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/genresTextBackground"
android:padding="@dimen/padding_genres"
android:text="@string/western"
android:textColor="@color/genresTextColor"
android:textSize="@dimen/text_size"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
一种方法是创建 item_layout.xml
并在下面添加:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/centerButton"
android:text="Mistery"/>
<Button
android:id="@+id/centerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Romance"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/centerButton"
android:text="Science Fiction"/>
</RelativeLayout>
然后每次添加内容时都可以将其用作行项目布局。希望对你有帮助