3

我尝试在其他视图下进行查看,我的问题是它似乎不起作用。他们在我的代码中查看的视图不在下方。firstText并没有在我输入的secondTextID 下进行。感谢您的帮助:)

public void setHelper() {

linear1 = (LinearLayout) findViewById(R.id.checkHelp1);
linear2 = (LinearLayout) findViewById(R.id.layout2);

firstText = (TextView) findViewById(R.id.textView4);
secondText = (TextView) findViewById(R.id.timer);

linear1.setVisibility(View.GONE);
linear2.setVisibility(View.GONE);

            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp2.addRule(RelativeLayout.BELOW, R.id.checkNeedHelp);
            firstText.setLayoutParams(lp2);

            lp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp2.addRule(RelativeLayout.BELOW, R.id.addNewExercise);
            lp2.setMargins(0, 25, 0, 0);
            secondText.setLayoutParams(lp2);

}

XML 代码

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

    <EditText
        android:id="@+id/editRoutine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/routines"
        android:ems="10"
        android:hint="add new routine"
        android:textColorHint="@color/black" >
    </EditText>

    <TextView
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/layout2"
        android:text="Routine exercsies" />

    <Spinner
        android:id="@+id/routineExercises"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/timer"
        android:layout_toLeftOf="@+id/deleteExerciseFromRoutine" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/routineExercises"
        android:text="Choose exercise to add to selected routine" />

    <Spinner
        android:id="@+id/exerciseForRoutine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_toLeftOf="@+id/addExercisesToRoutine" />

    <ImageButton
        android:id="@+id/addExercisesToRoutine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/exerciseForRoutine"
        android:layout_alignParentRight="true"
        android:src="@android:drawable/ic_input_add" />

    <ImageButton
        android:id="@+id/addNewRoutine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/routines"
        android:src="@android:drawable/ic_input_add" />

    <ImageButton
        android:id="@+id/deleteExerciseFromRoutine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignParentRight="true"
        android:src="@drawable/close" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/deleteExerciseFromRoutine"
        android:layout_alignParentRight="true" />

    <ImageButton
        android:id="@+id/removeRoutine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/routines"
        android:layout_toRightOf="@+id/routineExercises"
        android:src="@drawable/close" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Edit Routines"
        android:textSize="30sp" />

    <LinearLayout
        android:id="@+id/checkHelp1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkNeedHelp"
        android:background="@color/gray"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Manage routines"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add new routines or delete one from the list" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editRoutine"
        android:layout_marginTop="25sp"
        android:background="@color/gray"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Routine exercies"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add exercies to selected routine or delete one" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/swipeLayOut2"
        android:layout_width="110sp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@color/Black"
        android:orientation="vertical"
        android:visibility="gone" >

        <Button
            android:id="@+id/goHomePage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="home page"
            android:textSize="15sp" />

        <Button
            android:id="@+id/goCalendar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Calendar"
            android:textSize="15sp" />

        <Button
            android:id="@+id/goLive"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="live workout"
            android:textSize="15sp" />

        <Button
            android:id="@+id/goToday"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Today log"
            android:textSize="15sp" />

        <Button
            android:id="@+id/goProgram"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Program plan"
            android:textSize="15sp" />

        <Button
            android:id="@+id/goPersonal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pesonal detials"
            android:textSize="15sp" />

        <Button
            android:id="@+id/goWorkout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Set workout"
            android:textSize="15sp" />
    </LinearLayout>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/checkHelp1"
        android:text="Routines" />

    <Spinner
        android:id="@+id/routines"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView4"
        android:layout_toLeftOf="@+id/removeRoutine" />

    <CheckBox
        android:id="@+id/checkNeedHelp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="25sp"
        android:text="Need help?"
        android:textSize="13sp" />

    <com.example.workoutlog.VerticalTextView
                android:id="@+id/swipeImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/swipeLayOut2"
                android:background="@color/red"
                android:text="swipe right"
                android:textSize="18sp" />

</RelativeLayout>
4

1 回答 1

1

Layouts以自上而下的方式处理他们的孩子,因此 anylayout_abovelayout_below必须在布局文件中引用其上方的物理视图。

所以在使用 layout_below 之前把相关视图的 xml 放好。

下面是一个例子:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/contribution_amount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:textSize="72sp"
            android:text="77"
            tools:text="77"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toStartOf="@id/contribution_amount"
            android:paddingTop="12sp"
            android:textSize="28sp"
            android:text="$"
            tools:text="$"/>
    </RelativeLayout>

请注意,带有 layout_toStartOf 的 TextView 引用了一个在其之前定义的视图。如果我将 TextView 与 layout_toStartOf 元素放在 contribution_amount TextView 之前,则布局将不起作用。

于 2013-06-04T17:49:56.630 回答