0

在我的應用程序中,我有一個表單,用戶輸入他/她的地址。我有幾個EditTexts來輸入城市,州和郵編的信息。我有一個字段國家的線性佈局,由Country Picker取代。顯示下拉菜單,但拉伸線性佈局會使窗體看起來很糟糕。我一直無法讓它看起來很好。有時,如果我進行更改,下拉菜單不會顯示。請幫助我,因爲我是構建Android應用程序的初學者。調整佈局文件,使表單看起來更好

這裏是佈局的XML文件的一部分:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout android:id="@+id/Rlayout_recipient" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:layout_below="@id/top_bar_view" android:background="@drawable/bg"> 

    <EditText android:id="@+id/edt_rec_addr1" 
     android:layout_width="400dp" android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/edt_rec_name" 
     android:layout_alignRight="@+id/edt_rec_name" android:layout_below="@+id/edt_rec_name" 
     android:layout_marginTop="15dp" android:background="@drawable/border_email" 
     android:ems="10" android:hint="@string/addr1_hint" android:inputType="text" 
     android:padding="6dp" android:textColor="#FFFFFF" 
     android:textColorHint="#FFFFFF" /> 

    <EditText android:id="@+id/edt_rec_name" 
     android:layout_width="400dp" android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" android:layout_centerHorizontal="true" 
     android:layout_marginTop="22dp" android:background="@drawable/border_email" 
     android:ems="10" android:hint="@string/name_hint" android:inputType="text" 
     android:padding="6dp" android:textColor="#FFFFFF" 
     android:textColorHint="#FFFFFF" /> 

    <LinearLayout android:id="@+id/addr_layout" 
     android:layout_width="400dp" android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/edt_rec_addr2" 
     android:layout_alignParentBottom="true" android:layout_marginBottom="16dp" 
     android:background="@drawable/border_email"> 

     <EditText android:id="@+id/edt_rec_city" 
      android:layout_width="70dp" android:layout_height="wrap_content" 
      android:ems="10" android:hint="@string/city_hint" android:inputType="text" 
      android:padding="6dp" android:textColor="#FFFFFF" 
      android:textColorHint="#FFFFFF" /> 

     <EditText android:id="@+id/edt_rec_state" 
      android:layout_width="70dp" android:layout_height="wrap_content" 
      android:ems="10" android:hint="@string/state_hint" 
      android:inputType="text" android:padding="6dp" android:textColor="#FFFFFF" 
      android:textColorHint="#FFFFFF" /> 

     <EditText android:id="@+id/edt_rec_postcode" 
      android:layout_width="105dp" android:layout_height="wrap_content" 
      android:ems="10" android:hint="@string/postcode_hint" 
      android:inputType="number" android:padding="6dp" android:textColor="#FFFFFF" 
      android:textColorHint="#FFFFFF" /> 

     <LinearLayout android:id="@+id/country_layout" 
      android:layout_width="150dp" android:layout_height="wrap_content" 
      android:layout_marginRight="20dp" android:orientation="vertical" 
      android:weightSum="1" /> 


    </LinearLayout> 

    <EditText android:id="@+id/edt_rec_addr2" 
     android:layout_width="400dp" android:layout_height="wrap_content" 
     android:layout_above="@+id/addr_layout" android:layout_alignLeft="@+id/edt_rec_addr1" 
     android:layout_marginBottom="17dp" android:background="@drawable/border_email" 
     android:ems="10" android:hint="@string/addr2_hint" android:inputType="text" 
     android:padding="6dp" android:textColor="#FFFFFF" 
     android:textColorHint="#FFFFFF" /> 
</RelativeLayout> 

這是該活動的Java代碼:

public class RecipientAddressActivity extends FragmentActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_recipient_address);   
    FragmentManager manager = getSupportFragmentManager(); 
    FragmentTransaction transaction = manager.beginTransaction(); 

    CountryPicker picker = new CountryPicker(); 
    picker.setListener(new CountryPickerListener() { 

     @Override 
     public void onSelectCountry(String name, String code) { 
      Toast.makeText(
        RecipientAddressActivity.this, 
        "Country Name: " + name + " - Code: " + code 
            + " - Currency: " 
            + CountryPicker.getCurrencyCode(code), 
        Toast.LENGTH_SHORT).show(); 

     } 
    }); 

    transaction.replace(R.id.country_layout, picker); 
    transaction.commit(); 

} 

這裏是截圖的鏈接它的外觀

enter image description here

,它應該是怎樣看:

enter image description here

請,請幫助我。

回答

0

解決了這個問題,通過用edittext和打開國家選擇器的click事件替換線性佈局。

edt_ctry = (EditText) findViewById(R.id.edt_ctry); 
    edt_ctry.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      showCountryPicker(); 

     } 
    }); 

protected void showCountryPicker() { 
    final CountryPicker picker = CountryPicker.newInstance("Select Country"); 
    picker.setListener(new CountryPickerListener() { 

     @Override 
     public void onSelectCountry(String name, String code) { 
      edt_ctry.setText(name); 
      picker.dismiss(); 
     } 
    }); 

    picker.show(getSupportFragmentManager(), "COUNTRY_PICKER"); 

} 
0

它更好地使用垂直方向的線性佈局。我已經更改了一些代碼套件您的要求:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/Rlayout_recipient" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_below="@id/top_bar_view" 
    android:orientation="vertical" 
    android:background="@drawable/bg" > 

    <EditText 
     android:id="@+id/edt_rec_addr1" 
     android:layout_width="400dp" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/edt_rec_name" 
     android:layout_alignRight="@+id/edt_rec_name" 
     android:layout_below="@+id/edt_rec_name" 
     android:layout_marginTop="15dp" 
     android:background="@drawable/border_email" 
     android:ems="10" 
     android:hint="@string/addr1_hint" 
     android:inputType="text" 
     android:padding="6dp" 
     android:textColor="#FFFFFF" 
     android:textColorHint="#FFFFFF" /> 

    <EditText 
     android:id="@+id/edt_rec_name" 
     android:layout_width="400dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:background="@drawable/border_email" 
     android:ems="10" 
     android:hint="@string/name_hint" 
     android:inputType="text" 
     android:padding="6dp" 
     android:textColor="#FFFFFF" 
     android:textColorHint="#FFFFFF" /> 

    <LinearLayout 
     android:id="@+id/addr_layout" 
     android:layout_width="400dp" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/edt_rec_addr2" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="16dp" 
     android:background="@drawable/border_email" > 

     <EditText 
      android:id="@+id/edt_rec_city" 
      android:layout_width="70dp" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:hint="@string/city_hint" 
      android:inputType="text" 
      android:padding="6dp" 
      android:textColor="#FFFFFF" 
      android:textColorHint="#FFFFFF" /> 

     <EditText 
      android:id="@+id/edt_rec_state" 
      android:layout_width="70dp" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:hint="@string/state_hint" 
      android:inputType="text" 
      android:textColor="#FFFFFF" 
      android:textColorHint="#FFFFFF" /> 

     <EditText 
      android:id="@+id/edt_rec_postcode" 
      android:layout_width="105dp" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:hint="@string/postcode_hint" 
      android:inputType="number" 
      android:padding="6dp" 
      android:textColor="#FFFFFF" 
      android:textColorHint="#FFFFFF" /> 

     <LinearLayout 
      android:id="@+id/country_layout" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="20dp" 
      android:orientation="vertical" 
      android:weightSum="1" /> 
    </LinearLayout> 

    <EditText 
     android:id="@+id/edt_rec_addr2" 
     android:layout_width="400dp" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/addr_layout" 
     android:layout_alignLeft="@+id/edt_rec_addr1" 
     android:layout_marginBottom="17dp" 
     android:background="@drawable/border_email" 
     android:ems="10" 
     android:hint="@string/addr2_hint" 
     android:inputType="text" 
     android:textColor="#FFFFFF" 
     android:textColorHint="#FFFFFF" /> 

</RelativeLayout> 

希望它有幫助。

+0

使用線性佈局,您不能在上圖中顯示android:layout_alignLeft,layout_alignRight,below,above等元素。 – user2688158

+0

您可以將它們移除,因爲它們在線性佈局中不起作用。嘗試使用重量,重力或layout_gravity來調整組件。 –

+0

感謝您的回覆。使用線性佈局不會有幫助,它是一樣的。我想知道如果我可以讓國家成爲EditText,並且在它的觸摸事件中我打開了國家選擇器。如果您對此有任何建議,請告知我。謝謝。 – user2688158

相關問題