2017-01-28 6 views
0

Я только начинаю с разработки Android и стараюсь изучить тонкости всего. Я пытаюсь создать представление, что-то вроде $________ (знак доллара, за которым следует текстовое поле), но я не могу заставить LinearLayout работать правильно (я представляю, что он работает так же, как StackPanel XAML?).Расположение LinearLayout в android

Вот что я получил:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="75dp"> 
    <TextView android:text="$" 
       android:textSize="65dp" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 
    <EditText android:layout_width="150dp" 
     android:layout_height="match_parent"/> 
    </LinearLayout> 
</LinearLayout> 

Но только знак доллара показывает вверх. Если я включаю его таким:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="75dp"> 
    <EditText android:layout_width="150dp" 
     android:layout_height="match_parent"/> 
    <TextView android:text="$" 
       android:textSize="65dp" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 
    </LinearLayout> 
</LinearLayout> 

тогда он показывает, как я бы ожидать: _________$, а наоборот, кажется, не работает хорошо. Любая помощь очень ценится!

ответ

1

вы можете использовать

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView android:text="$" 
     android:textSize="65sp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <EditText 
     android:textSize="65sp" 
     android:hint="Edit text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

И

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:textSize="65sp" 
     android:layout_width="wrap_content" 
     android:hint="Edit text" 
     android:layout_height="wrap_content"/> 

     <TextView android:text="$" 
      android:textSize="65sp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

</LinearLayout> 
0

Вы должны использовать wrap_content для layout_width of TextView.

0

Вам нужно не соответствовать ширине родителя на знаке доллара.

Как насчет относительной компоновки?

Или вы можете установить ширину 0dp на знак доллара и поле ввода, а затем установить атрибут веса макета. Тогда LinearLayout имеет атрибут веса.

0

Эй пытаются wrap_content ширину TextView

<TextView android:text="$" 
        android:textSize="65dp" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent"/> 
    <EditText android:layout_width="150dp" 
     android:layout_height="match_parent"/>