垂直居中时,如何考虑行动吧?

时间:2016-03-20 07:33:46

标签: android layout alignment

我正确地居中,正如您在查看content_main.xml设计标签时所看到的那样,

content_main.xml

但是,当你看到activity_main.xml设计时,操作栏就位,当它仍然在应用程序的中心时,它不在空白中心,这是我更喜欢的。有关如何计算操作栏的任何建议?如果可能,最好在XML中。

activity_main.xml

这是我的XML

var data = [{ known_technologies : ["prepaid billing", " postpaid billing"],company : "A",YearsOfExp:0},
{ known_technologies : ["prepaid billing", " postpaid billing"],company:"B",YearsOfExp:1}];

1 个答案:

答案 0 :(得分:0)

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/voting_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_vertical|center"
    android:background="@color/colorFaded"
    android:configChanges="orientation|screenSize"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:screenOrientation="nosensor"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gesslar.threshvote.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/textStatusMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/colorText" />

    <ImageButton
        android:id="@+id/buttonVote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:background="@android:color/transparent"
        android:clickable="false"
        android:contentDescription="@string/desc_vote_button"
        android:onClick="onVotePressed"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_vote_button_image" />

    <TextSwitcher
        android:id="@+id/textCountdown"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/buttonVote"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:includeFontPadding="false"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/colorText"
        android:textSize="@dimen/countdown" />

    <ProgressBar
        android:id="@+id/loadingBar"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/textCountdown"
        android:layout_gravity="end"
        android:indeterminate="true"
        android:visibility="invisible" />
</RelativeLayout>

但是你需要在设备或模拟器上运行它。 Android Studio预览显示错误。关键是app:layout_behavior="@string/appbar_scrolling_view_behavior"

修改
您还需要使用RelativeLayout - 它支持父级的良好居中。在LinearLayout中,只有当ImageView为match_parent。

时,您才能将图片居中