java - Which is a good solution to center the layouts in this way?(android xml) -
i don't want use many linear layouts set gravity center. there better solution? thanks! 
this solution linear layouts told me need make using relative layout because using many linear layouts causes slow rendering, did not succed relative layouts.
<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>
one way doing create item_layout.xml , add below:
<?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> then everytime u add u can use row item layout. hope helps
Comments
Post a Comment