0

線形レイアウトを学習していますが、混乱しています。

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


    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_weight=".33"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button" 
        android:layout_weight=".33"/> 


     <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"   
        android:text="Button" 
        android:layout_weight=".3"/>


</LinearLayout>

これらのコードには 3 つの要素があります。

  1. TextView 2.Button1 3.Button2

線形レイアウトの向きは垂直で、1 つの重み値のみを調整して同じ重みを与えています。

今、私は与えている垂直方向に

android:layout_width="fill_parent"
android:layout_height="wrap_content"

したがって、高さと幅の両方を埋めています。

しかし、水平レイアウトを使用して次のように書いている場合

    android:layout_height="fill_parent"
    android:layout_width="wrap_content"

幅を埋めますが、高さを埋めません

Android:layout_height="fill_parent" を与える代わりに、なぜ高さを埋めないのですか?

4

2 に答える 2

0

このコードをコピーして貼り付けて試してみてください.高さがいっぱいになります。

<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"
   android:orientation="horizontal"
    tools:context=".MainActivity" >   


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="@string/hello_world" 
        android:layout_weight=".33"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="Button" 
        android:layout_weight=".33"/> 


     <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"   
        android:text="Button" 
        android:layout_weight=".3"/>


</LinearLayout>
于 2013-03-15T10:02:01.743 に答える
0

I think you need an understanding on this topic instead of someone throwing a code at you. The working code is as shown by @Nirali above.

So, in this crash course, you need to understand what is fill_parent and wrap_content. The linearlayout is your parent or you can see this as the screen of your device. If you set your height to fill_parent, it will fill the whole height of the screen.

Wrap_content is to wrap your object, for example your button. If you set height to wrap_content, it will just set the height to wrapping your button nicely.

Above are explanation that comes off my head, for a better understanding, refer to this post What's the difference between fill_parent and wrap_content?

于 2013-03-15T10:12:48.827 に答える