0

默认情况下,在我当前的布局中,顶部有两个 TextView,底部有一个 ImageView。我想通过使用 Java 让 ImageView 位于顶部而 TextViews 位于底部来改变这一点。请帮忙。

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

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="2" >

    <TextView
        android:id="@+id/tvText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/text_view"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textSize="13dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvText2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_weight="1"
       android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:background="@drawable/text_view"
        android:textSize="13dp"
        android:textStyle="bold"
         />
</LinearLayout>
</FrameLayout>

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/text_view"
        android:scaleType="fitXY" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
4

1 回答 1

1

这是我所做的:我在 xml 中添加了另外 2 个框架布局,当您单击第一个文本视图时,它将更改视图,我的意思是上面的框架布局并将显示接下来的两个。希望这对您有所帮助

public class MainActivity extends Activity

{

    LinearLayout ll;
    TextView tv;

    FrameLayout ff2, ff1, ff3, ff4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ff1 = (FrameLayout) findViewById(R.id.lay1);
        ff2 = (FrameLayout) findViewById(R.id.lay2);
        ff3 = (FrameLayout) findViewById(R.id.lay3);
        ff4 = (FrameLayout) findViewById(R.id.lay4);
        ff3.setVisibility(View.GONE);
        ff4.setVisibility(View.GONE);

        tv = (TextView) findViewById(R.id.tvText);
        tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ff1.setVisibility(View.GONE);
                ff2.setVisibility(View.GONE);
                ff3.setVisibility(View.VISIBLE);
                ff4.setVisibility(View.VISIBLE);
                Toast.makeText(getApplicationContext(), "layout change",
                        Toast.LENGTH_LONG).show();

            }
        });

    }

}

这是布局文件:

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

    <FrameLayout
        android:id="@+id/lay1"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:weightSum="2" >

            <TextView
                android:id="@+id/tvText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/weed2"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="13dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvText2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/weed2"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="13dp"
                android:textStyle="bold" />
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/lay2"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/ivImage"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:background="@drawable/ic_launcher"
                android:scaleType="fitXY" />
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/lay3"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/ivImage"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:background="@drawable/ic_launcher"
                android:scaleType="fitXY" />
        </LinearLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/lay4"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:weightSum="2" >

            <TextView
                android:id="@+id/tvText"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/weed2"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="13dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvText2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/weed2"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textSize="13dp"
                android:textStyle="bold" />
        </LinearLayout>
    </FrameLayout>

</LinearLayout>
于 2013-07-26T09:40:51.123 回答