[英]Android - How to make textfields center alligned for different android devices
I am working on android application.I am newbie in android app development. 我正在开发android应用程序。我是android应用程序开发的新手。 I get to test the app for few android devices. 我可以测试一些Android设备的应用程序。 But the layout looks different for different devices. 但是对于不同的设备,布局看起来有所不同。 i have created screen shots for reference. 我创建了屏幕截图以供参考。
CODE: 码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bgcopy"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
style="@style/TitleBar" >
<Button
android:id="@+id/back"
android:layout_width="60dp"
android:layout_height="30dip"
android:layout_marginLeft="7dip"
android:background="@drawable/bround1"
android:text="@string/back"
android:textColor="@color/White"
android:textStyle="bold" />
<TextView
style="@style/TitleBarText"
android:gravity="center"
android:text="@string/testApp"
android:textSize="16dp" />
<Button
android:id="@+id/home"
android:layout_width="60dip"
android:layout_height="30dip"
android:layout_marginRight="7dip"
android:background="@drawable/bround1"
android:text="@string/home"
android:textColor="@color/White"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1.5dp"
android:background="@color/Black" >
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignBottom="@+id/relativeLayout1"
android:orientation="horizontal"
android:background="@drawable/blackroundshape"
android:gravity="center">
<TextView
android:id="@+id/restrictions"
android:layout_width="115dip"
android:layout_height="54dip"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:background="@drawable/blackroundshape"
android:gravity="center"
android:layout_gravity="center"
android:selectAllOnFocus="true"
android:text="Details"
android:textColor="@color/White"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/locations"
android:layout_width="115dip"
android:layout_height="54dip"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:background="@drawable/blackroundshape"
android:gravity="center"
android:layout_gravity="center"
android:text="@string/locations"
android:textColor="@color/White"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/news"
android:layout_width="128dip"
android:layout_height="54dip"
android:layout_marginLeft="0.5dip"
android:layout_weight="1"
android:background="@drawable/blackroundshape"
android:gravity="center"
android:layout_gravity="center"
android:selectAllOnFocus="true"
android:text="@string/news"
android:textColor="@color/White"
android:textSize="15dip"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@+id/linearLayout1"
android:orientation="horizontal"
android:background="@drawable/backbutton"
android:gravity="center"
>
<TextView
android:id="@+id/reserve"
android:layout_width="115dip"
android:layout_height="58dip"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:background="@drawable/backbutton"
android:gravity="center"
android:layout_gravity="center"
android:text="@string/reserve"
android:textColor="@color/White"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/favorite"
android:layout_width="115dip"
android:layout_height="58dip"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:background="@drawable/backbutton"
android:gravity="center"
android:layout_gravity="center"
android:text="@string/favorite"
android:textColor="@color/White"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/share"
android:layout_width="128dip"
android:layout_height="58dip"
android:layout_weight="1"
android:background="@drawable/backbutton"
android:gravity="center"
android:layout_gravity="center"
android:selectAllOnFocus="true"
android:text="@string/share"
android:textColor="@color/White"
android:textSize="15dip"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="504dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" >
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="170dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="7dp"
android:background="@drawable/round_transperent2" >
<ImageView
android:id="@+id/image"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="11dp"
android:src="@drawable/default_ptr_rotate" />
<TextView
android:id="@+id/couponText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/image"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_toRightOf="@+id/image"
android:text="TextView"
android:textColor="@color/Black"
android:typeface="serif" />
<TextView
android:id="@+id/save_textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/image"
android:layout_below="@+id/image"
android:layout_marginLeft="1dp"
android:paddingLeft="1dp"
android:text="@string/save"
android:textColor="@color/mightyBlue"
android:textSize="19sp"
android:textStyle="bold" />
<TextView
android:id="@+id/upto_textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/save_textView2"
android:layout_alignBottom="@+id/save_textView2"
android:layout_alignRight="@+id/image"
android:layout_marginLeft="3dp"
android:layout_toRightOf="@+id/save_textView2"
android:text="@string/upto"
android:textColor="@color/Gray" />
<TextView
android:id="@+id/amountDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/save_textView2"
android:layout_below="@+id/save_textView2"
android:layout_marginBottom="4dp"
android:paddingLeft="1dp"
android:text="@string/amount"
android:textColor="@color/mightyBlue"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/amountDetails"
android:layout_alignLeft="@+id/couponText"
android:text="Expires on:"
android:textColor="@color/Black"
android:typeface="serif"
android:visibility="invisible" />
<TextView
android:id="@+id/expitydate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/amountDetails"
android:layout_toRightOf="@+id/textView5"
android:maxLength="10"
android:textColor="@color/Black"
/>
</RelativeLayout>
<TextView
android:id="@+id/RemainingDays_textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView7"
android:layout_below="@+id/relativeLayout2"
android:text="Remaning Days"
android:textColor="@color/mightyBlue" />
<TextView
android:id="@+id/textView6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/RemainingDays_textView1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="@string/redeem"
android:textColor="@color/Black"
android:textSize="16dp"/>
<TextView
android:id="@+id/textView7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/textView6"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:text="@string/redeemnow"
android:textColor="@color/Red"
android:textSize="18dp" />
<TextView
android:id="@+id/YES"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignLeft="@+id/textView7"
android:layout_below="@+id/textView7"
android:layout_marginLeft="35dp"
android:layout_marginTop="20dp"
android:background="@drawable/yellow_ball"
android:gravity="center"
android:text="YES"
android:textColor="@color/Black"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/No"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_alignBaseline="@+id/YES"
android:layout_alignBottom="@+id/YES"
android:layout_alignRight="@+id/textView7"
android:layout_marginRight="44dp"
android:background="@drawable/yellow_ball"
android:gravity="center"
android:text="NO"
android:textColor="@color/Black"
android:textSize="16dp"
android:textStyle="bold" />
</RelativeLayout>
2nd screen shot shows the issue. 第二屏幕截图显示了该问题。 I have completed the functional aspect of the app but unable to submit app due to this issue. 我已经完成了应用程序的功能方面,但由于此问题无法提交应用程序。 Any help will be appriciated. 任何帮助将被申请。
Thank You!!! 谢谢!!!
The containing LinearLayout should have gravity = center to center the TextView children and android:layout_alignParentBottom="true"
(I see you have a RelativeLayout as a parent container): 包含的LinearLayout应该具有重心= center以使TextView子级and android:layout_alignParentBottom="true"
(我看到您有一个RelativeLayout作为父容器):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/navBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/restrictions"
android:layout_width="115dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:selectAllOnFocus="true"
android:text="Details"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/locations"
android:layout_width="115dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:text="Locations"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/news"
android:layout_width="128dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginLeft="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:selectAllOnFocus="true"
android:text="news"
android:textSize="15dip"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/navBar"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/restrictions"
android:layout_width="115dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:selectAllOnFocus="true"
android:text="Details"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/locations"
android:layout_width="115dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:text="Locations"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/news"
android:layout_width="128dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginLeft="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:selectAllOnFocus="true"
android:text="news"
android:textSize="15dip"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/restrictions"
android:layout_width="115dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:selectAllOnFocus="true"
android:text="Details"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/locations"
android:layout_width="115dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginRight="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:text="Locations"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="@+id/news"
android:layout_width="128dip"
android:layout_height="54dip"
android:layout_gravity="center"
android:layout_marginLeft="0.5dip"
android:layout_weight="1"
android:gravity="center"
android:selectAllOnFocus="true"
android:text="news"
android:textSize="15dip"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Have added different layouts for different screen sizes? 是否为不同的屏幕尺寸添加了不同的布局?
Refer : http://developer.android.com/guide/practices/screens_support.html 请参阅: http : //developer.android.com/guide/practices/screens_support.html
For Different screen size, The following is a list of resource directories in an application that provides different layout designs for different screen sizes and different bitmap drawables for small, medium, high, and extra high density screens. 对于不同的屏幕尺寸,以下是应用程序中的资源目录列表,该应用程序为不同的屏幕尺寸提供了不同的布局设计,并为小,中,高和超高密度屏幕提供了不同的位图可绘制对象。
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
The following code in the Manifest supports all dpis. 清单中的以下代码支持所有dpi。
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
Try this, 尝试这个,
using layout_weight attribute you can bring this exactly. 使用layout_weight属性,您可以准确地实现这一点。
Double check to make sure you have the correct hierarchy with the proper layouts wrapping other layouts. 仔细检查以确保您具有正确的层次结构,并且正确的布局包装了其他布局。 For the 2nd bar from the top with "Reserve, Favorite, Share" use a Linear Layout with weights of 1 on the text views and be sure that the TV's width = 0dp. 对于顶部带有“保留,收藏,共享”的第二栏,请在文本视图上使用权重为1的线性布局,并确保电视的宽度= 0dp。 Then create the bottom bar with "Details, Location, News" and align that to the bottom. 然后使用“详细信息,位置,新闻”创建底部栏,并将其与底部对齐。 Then have the middle white part as a layout with attributes layout_below="topbar" and layout_above="bottombar" 然后将中间的白色部分作为具有属性layout_below =“ topbar”和layout_above =“ bottombar”的布局
Here's some sudo code to give you an idea 这是一些sudo代码可以给你一个想法
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- Top Bar -->
<LinearLayout
andorid:name="@+id/nav_bar"
android:layout_alignParentTop="true"
android:layout_width=... />
<!-- Second to Top Bar -->
<LinearLayout
android:name="@+id/reserve_bar"
android:layout_width="fill_parent"
android:layout_height="54dp"
android:layout_below="@id/nav_bar"
android:orientation="vertical" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
... />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
... />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
... />
</LinearLayout>
<!-- Very Bottom Bar -->
<LinearLayout
android:name="@+id/details_bar"
android:layout_width="fill_parent"
android:layout_height="54dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
... />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
... />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
... />
</LinearLayout>
<!-- Body of the layout below 2nd bar and above bottom bar-->
<RelativeLayout
android:name="@+id/body_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/reserve_bar"
android:layout_above="@id/detail_bar" >
<RelativeLayout>
android:stuff="..."
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
I think the problem is that you are have align_bottom attributes on some things that shouldn't be there. 我认为问题在于您在某些不应该存在的东西上具有align_bottom属性。 Those are override the alignParentBottom 这些被覆盖alignParentBottom
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.