无法在全高度扩展可扩展布局

时间:2017-06-22 09:21:58

标签: java android xml

我开发了一个应用程序,其中包含可扩展布局。 理想情况下,界面应该看起来像这样

example in the documentation

但在我的应用程序中,可扩展布局并未在全高度打开。仅显示布局的某些部分。如果我为可扩展布局提供更多高度,则imageview将占据所有高度。只有imageview受到影响并变大。

enter image description here

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:overScrollMode="ifContentScrolls"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="uz.fido_biznes.xalqbanki.HomeActivity"
    tools:showIn="@layout/app_bar_home">




    <LinearLayout
        android:id="@+id/layout_cards"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".25"
        android:background="#AED581"
        android:orientation="horizontal"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_card_general"
            android:layout_gravity="center_vertical"
            android:layout_margin="16dp"
            />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="8dp"
            android:layout_gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cards"
                android:textColor="@color/home_text_color"
                android:textStyle="bold"
                android:textSize="24sp"
                />
            <TextView
                android:id="@+id/cards_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/cards_description"
                android:textColor="@color/home_text_color"/>
        </LinearLayout>
    </LinearLayout>




    <!--Collapsed layout for cards-->
    <LinearLayout
        android:id="@+id/collapse_cards"
        android:layout_width="match_parent"
        android:visibility="gone"
        android:orientation="vertical"
        android:layout_height="0dp"
        android:layout_weight=".8"
        >

        <android.support.v4.view.ViewPager
            android:id="@+id/cards_pager"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </android.support.v4.view.ViewPager>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="#9E9E9E" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

        </LinearLayout>

    </LinearLayout>


    <LinearLayout
        android:id="@+id/layout_credit"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".25"
        android:background="#90CAF9"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_kredit_general"
            android:layout_gravity="center_vertical"
            android:layout_margin="16dp"
            />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="8dp"
            android:layout_gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/credit"
                android:textColor="@color/home_text_color"
                android:textStyle="bold"
                android:textSize="24sp"
                />
            <TextView
                android:id="@+id/credit_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/credit_description"
                android:textColor="@color/home_text_color"/>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/layout_deposits"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".25"
        android:background="#FFE082"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_vklad_general"
            android:layout_gravity="center_vertical"
            android:layout_margin="16dp"
            />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="8dp"
            android:layout_gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/deposits"
                android:textColor="@color/home_text_color"
                android:textStyle="bold"
                android:textSize="24sp"
                />
            <TextView
                android:id="@+id/deposit_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/deposit_description"
                android:textColor="@color/home_text_color"/>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/layut_npc"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".25"
        android:background="#CFD8DC"
        >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_nps_general"
            android:layout_gravity="center_vertical"
            android:layout_margin="16dp"
            />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="8dp"
            android:layout_gravity="center_vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/nps"
                android:textColor="@color/home_text_color"
                android:textStyle="bold"
                android:textSize="24sp"
                />
            <TextView
                android:id="@+id/nps_description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="16sp"
                android:text="@string/nps_description"
                android:textColor="@color/home_text_color"/>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

爪哇:

    public class HomeActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
        LinearLayout cards, credit, deposit, nps, collapse_cards;
        TextView card_desc, credit_desc, deposit_desc, nps_desc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ViewPager pager = (ViewPager)findViewById(R.id.cards_pager);
        CardsSliderAdapter adapter = new 
CardsSliderAdapter(getApplicationContext());
        pager.setAdapter(adapter);
        pager.setCurrentItem(0);

        card_desc = (TextView)findViewById(R.id.cards_description);
        credit_desc = (TextView)findViewById(R.id.credit_description);
        deposit_desc = (TextView)findViewById(R.id.deposit_description);
        nps_desc = (TextView)findViewById(R.id.nps_description);

        cards = (LinearLayout)findViewById(R.id.layout_cards);
        credit = (LinearLayout)findViewById(R.id.layout_credit);
        deposit = (LinearLayout)findViewById(R.id.layout_deposits);
        nps = (LinearLayout)findViewById(R.id.layut_npc);
        collapse_cards = (LinearLayout)findViewById(R.id.collapse_cards);
        cards.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(collapse_cards.getVisibility() == View.GONE){
                    expand(collapse_cards);
                    card_desc.setVisibility(View.GONE);
                    credit_desc.setVisibility(View.GONE);
                    deposit_desc.setVisibility(View.GONE);
                    nps_desc.setVisibility(View.GONE);
                }else {
                    collapse(collapse_cards);
                }
            }
        });
        FloatingActionButton fab = (FloatingActionButton) 
findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), 
RegistrationActivity.class);
                startActivity(intent);
            }
        });

        DrawerLayout drawer = (DrawerLayout) 
findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, 
R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) 
findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        BranchesOpenHelper branchesOpenHelper = new 
BranchesOpenHelper(this);
        SQLiteDatabase db = branchesOpenHelper.getReadableDatabase();


    }





    private void expand(LinearLayout lay)
    {
        lay.setVisibility(View.VISIBLE);

        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, 
View.MeasureSpec.UNSPECIFIED);
        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, 
View.MeasureSpec.UNSPECIFIED);
        lay.measure(widthSpec, heightSpec);

        ValueAnimator mAnimator = slideAnimator(0, 
credit.getMeasuredHeight(), lay);
        mAnimator.start();
    }

    private void collapse(final LinearLayout lay) {
        int finalHeight = lay.getHeight();

        ValueAnimator mAnimator = slideAnimator(finalHeight, 0, lay);

        mAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                //Height=0, but it set visibility to GONE
                lay.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }

        });
        mAnimator.start();
    }

    private ValueAnimator slideAnimator(int start, int end, final 
 LinearLayout lay)
    {

        ValueAnimator animator = ValueAnimator.ofInt(start, end);

        animator.addUpdateListener(new 
ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //Update Height
                int value = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = lay.getLayoutParams();
                layoutParams.height = value;
                lay.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }



    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) 
findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is 
present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_back) {
            onBackPressed();
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) 
findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

1 个答案:

答案 0 :(得分:0)

尝试这样做:

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
>
<android.support.v4.view.ViewPager
    android:id="@+id/cards_pager"
    android:layout_width="wrap_content"
    android:padding="16dp"
    android:layout_height="0dp"
    android:layout_weight=".75"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:padding="16dp"
    android:background="#9E9E9E" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".25"
    android:padding="8dp"
    android:orientation="horizontal">
 ....
</LinearLayout>