有幾個已經回答了關於android視圖從xml文件的繪製順序的問題,我已經閱讀並嘗試了他們的解決方案,但沒有運氣。我有一個有三個孩子的RelativeLayout,一個ImageView,一個Button和一個LinearLayout。我希望LinearLayout處於最佳狀態,其他兩個處於後臺。根據Defining Z order of views of RelativeLayout in Android,兄弟姐妹按照從xml文件中讀取的順序繪製,所以我將LinearLayout放在文件的最後,但仍然繪製在後面。下面是我的文件:Android RelativeLayout查看訂單
<RelativeLayout
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<ImageView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/register_button"
android:paddingLeft="@dimen/padding_xxlarge"
android:paddingRight="@dimen/padding_xxlarge"
android:src="@drawable/logo"/>
<Button
android:layout_width="fill_parent"
android:layout_height="@dimen/element_xlarge"
android:text="@string/action_register"
android:id="@+id/register_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:id="@+id/email_login_form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="@+id/register_button"
android:layout_marginBottom="-40dp">
<AutoCompleteTextView
android:id="@+id/net_id"
android:layout_width="fill_parent"
android:layout_height="@dimen/element_small"
android:hint="@string/prompt_username"
android:inputType="textAutoComplete"
android:maxLines="1"
android:singleLine="true"
android:layout_marginLeft="@dimen/padding_xlarge"
android:layout_marginRight="@dimen/padding_xlarge"
android:layout_marginBottom="@dimen/padding_small" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="@dimen/element_small"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:layout_marginLeft="@dimen/padding_xlarge"
android:layout_marginRight="@dimen/padding_xlarge" />
<Button
android:id="@+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/padding_xlarge"
android:layout_marginRight="@dimen/padding_xlarge"
android:text="@string/action_sign_in" />
</LinearLayout>
圖片: https://drive.google.com/a/cornell.edu/file/d/0BzEvKm6_q_oqbllRd284dS1ubVk/view?usp=sharing
我已經使用bringToFront()方法在我的onCreate()函數也試過:
findViewById(R.id.email_login_form).bringToFront();
但是,這並沒有影響。我做錯了什麼,以及如何讓我的LinearLayout出現在頂部?
--UPDATE--
測試從評論者提出了若干建議後,我發現這非常怪異的行爲,可能是問題的根源。當我添加一個測試按鈕,如下所示:
<RelativeLayout
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:clipChildren="false">
<Button
android:layout_width="fill_parent"
android:layout_height="@dimen/element_xlarge"
android:text="@string/action_register"
android:id="@+id/register_button"
android:layout_alignParentBottom="true"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@id/register_button"
android:paddingLeft="@dimen/padding_xxlarge"
android:paddingRight="@dimen/padding_xxlarge"
android:src="@drawable/logo"/>
<Button
android:id="@+id/test_button"
android:text="test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/register_button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="-30dp"/>
<LinearLayout
android:id="@+id/email_login_form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_above="@id/register_button"
android:layout_marginBottom="-30dp"
android:clipChildren="false" >
<AutoCompleteTextView
android:id="@+id/net_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_username"
android:inputType="textAutoComplete"
android:maxLines="1"
android:singleLine="true"
android:layout_marginLeft="@dimen/padding_xlarge"
android:layout_marginRight="@dimen/padding_xlarge"
android:layout_marginBottom="@dimen/padding_small" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_password"
android:imeActionId="@+id/login"
android:imeActionLabel="@string/action_sign_in_short"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:layout_marginLeft="@dimen/padding_xlarge"
android:layout_marginRight="@dimen/padding_xlarge" />
<Button
android:id="@+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/padding_xlarge"
android:layout_marginRight="@dimen/padding_xlarge"
android:text="@string/action_sign_in" />
</LinearLayout>
</RelativeLayout>
然後test_button繪製在register_button 和的的LinearLayout的頂部,儘管它的LinearLayout在XML之前被宣佈。爲什麼測試按鈕的處理方式與LinearLayout不同?
你能添加截圖?這可能有助於理解正在發生的事情。 [然後回覆,如果你添加它,所以我得到通知] – MDragon00 2014-12-07 18:45:20
這個'android:layout_above =「@ + id/register_button」'創建一個新的ID,當視圖仍然沒有被創建。因此,儘管您可以將它用於ImageView(但您最好先創建Button),但對於LinearLayout是錯誤的(使用'android:layout_above =「@ id/register_button」'代替)。 – 2014-12-07 18:46:23
截圖請。 – 2014-12-07 18:48:33