2015-08-16 107 views
-1

我是android開發新手。我做了一個簡單的登錄屏幕演示。我用Xml來做到這一點,並檢查了我在橫向和縱向模式下的輸出。它在縱向模式下看起來很好,但在風景上我的登錄按鈕不可見,我無法滾動我的視圖。我在我的xml文件中使用了dp,我認爲這是由於dp,我有這個問題。爲什麼按鈕不是顯示和文本視圖?

這裏是我的屏幕截圖,顯示顯示內容。 enter image description here

這是正在尋找精細肖像模式..

enter image description here

當我轉動我的設備移動到景觀,但它沒有顯示按鈕和文本下方看法?我可以添加滾動視圖嗎?我可以添加百分比而不是dp

這裏是我的代碼

<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" 
    tools:context=".MainActivity" 
    android:background="#223399" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_marginTop="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:textAppearanceLarge" 
     android:text="Login here" 
     android:gravity="center" 
     /> 


    <EditText 
     android:layout_marginTop="80dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Username" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     /> 
    <EditText 
     android:layout_marginTop="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Password" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     /> 

    <Button 
     android:layout_marginTop="80dp" 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:id="@+id/loginClick" 
     android:layout_gravity="center" 
     /> 
    <TextView 
     android:layout_marginTop="40dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:textAppearanceMedium" 
     android:text="New User?" 
     android:textColor="#00eeff" 
     android:gravity="center" 
     android:id="@+id/regiester_id" 
     /> 


</LinearLayout> 

Java代碼

public static final String MyPREFERENCES = "MyPrefs" ; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

我如何設置屏幕,以便它在肖像看起來好以及風景模式?

更新代碼

<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" 
    tools:context=".MainActivity" 
    android:background="#223399" 
    android:orientation="vertical"> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent"> 

    <TextView 
     android:layout_marginTop="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:textAppearanceLarge" 
     android:text="Login here" 
     android:gravity="center" 
     /> 


    <EditText 
     android:layout_marginTop="80dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Username" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     /> 
    <EditText 
     android:layout_marginTop="20dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Password" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     /> 

    <Button 
     android:layout_marginTop="80dp" 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:id="@+id/loginClick" 
     android:layout_gravity="center" 
     /> 
    <TextView 
     android:layout_marginTop="40dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:textAppearanceMedium" 
     android:text="New User?" 
     android:textColor="#00eeff" 
     android:gravity="center" 
     android:id="@+id/regiester_id" 
     /> 
    </ScrollView> 


</LinearLayout> 
+0

您可以使用'android:layout_weight'。 請參閱此:http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean –

+0

我知道佈局的重量我在這不使用它的東西 – user944513

+1

Scrollview必須有一個孩子只有不是多個孩子所以在滾動視圖中使用linearlayout。然後在該線性佈局中使用多個孩子。 – Pankaj

回答

0

當我轉動我的設備將其移動到景觀。它沒有顯示按鈕 及以下文本視圖?我可以添加滾動視圖?或者我可以添加% 百分比而不是dp。

我會說使用ScrollView爲更好的屏幕調整,它也將在更小的設備屏幕上有所幫助。

此外,您還可以針對每種尺寸的屏幕以及landscapeportrait製作不同類型的佈局。

有關屏幕布局的類型,請參閱開發人員的site

我希望你能從那裏獲得不同類型屏幕尺寸的信息。

+0

我明白我需要添加滾動視圖..ok我會嘗試並更新你 – user944513

+0

我更新代碼滾動視圖它使我的應用程序崩潰爲什麼/? – user944513

+0

你能告訴我爲什麼它崩潰了 – user944513

1

我給它一個使用layout_weight的快速嘗試。你可以使用不同的權重更好地調整它。

<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" 
    tools:context=".MainActivity" 
    android:background="#223399" 
    android:orientation="vertical" 
    android:weightSum="5"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:textAppearanceLarge" 
     android:text="Login here" 
     android:gravity="center" 
     android:layout_weight="1" 
     /> 


    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Username" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_weight="1" 
     /> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Password" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_weight="1" 
     /> 

    <Button 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Login" 
     android:id="@+id/loginClick" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:textAppearanceMedium" 
     android:text="New User?" 
     android:textColor="#00eeff" 
     android:gravity="center" 
     android:id="@+id/regiester_id" 
     android:layout_weight="1" 
     /> 


</LinearLayout> 
0

ScrollView只能有一個孩子,試圖改變這樣的代碼:

<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context=".MainActivity" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#223399"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <TextView 
       android:layout_marginTop="20dp" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:textAppearanceLarge" 
       android:text="Login here" 
       android:gravity="center" 
       /> 


      <EditText 
       android:layout_marginTop="80dp" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="Username" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       /> 
      <EditText 
       android:layout_marginTop="20dp" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="Password" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       /> 

      <Button 
       android:layout_marginTop="80dp" 

       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Login" 
       android:id="@+id/loginClick" 
       android:layout_gravity="center" 
       /> 
      <TextView 
       android:layout_marginTop="40dp" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:textAppearanceMedium" 
       android:text="New User?" 
       android:textColor="#00eeff" 
       android:gravity="center" 
       android:id="@+id/regiester_id" 
       /> 

     </LinearLayout> 
</ScrollView> 
0

滾動觀必須有一個孩子。因此,請用下面的代碼更新你的代碼,看看它的工作與否。

<ScrollView 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:background="#223399" 
     tools:context=".MainActivity" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="20dp" 
       android:gravity="center" 
       android:text="Login here" 
       android:textAppearance="?android:textAppearanceLarge" /> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_marginTop="80dp" 
       android:hint="Username" /> 

      <EditText 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_marginTop="20dp" 
       android:hint="Password" /> 

      <Button 
       android:id="@+id/loginClick" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_marginTop="80dp" 
       android:text="Login" /> 

      <TextView 
       android:id="@+id/regiester_id" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="40dp" 
       android:gravity="center" 
       android:text="New User?" 
       android:textAppearance="?android:textAppearanceMedium" 
       android:textColor="#00eeff" /> 
     </LinearLayout> 

    </ScrollView> 
相關問題