FrameLayout.LayoutParams不起作用

时间:2018-07-22 08:11:09

标签: android android-tablayout

我正在尝试避免在活动(screen shot of the activity)中出现以下情况。当我使用setMargins添加边距时,它不起作用。我已经尝试通过xml代码添加边距,但最终结果还是可以实现的,但是这非常耗时,并且无法保证它将对所有设备都适用。我该如何用代码做到这一点?

这是代码。

package com.example.android.tourguide;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.FrameLayout;

public class TouristAttractions extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu_item);
    /*
      Finding the Toolbar that is defined in activity_main.xml via the id toolbar.
      Note:The following three lines should be repeated for all the activities that are opened from the MainActivity. Also the toolbar should have an orientation
      which is not zero.
     */
    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar_for_menu_items);
    /*
       Setting the action bar as the toolbar defined in the above code line
     */
    setSupportActionBar(toolbar);
    /*
      Setting the title text color of the app bar.
     */
    toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.color_of_text_of_app_bar));
    mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
    /*
       Finding the drawerLayout so that when the user clicks on the menu item of the navigation drawer it should close as we invoke the method closeDrawers()
     */
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

            if (menuItem.getItemId() == R.id.home_menu) {
                /*
                Opening the home class that is the MainActivity when the Tourist Home menu button is clicked.
                 */
                Intent intentToOpenHomeClass = new Intent(TouristAttractions.this, MainActivity.class);
                startActivity(intentToOpenHomeClass);
            } else if (menuItem.getItemId() == R.id.entertainment_menu) {
                Intent intentToOpenEntertainmentClass = new Intent(TouristAttractions.this, Entertainment.class);
                startActivity(intentToOpenEntertainmentClass);
            } else if (menuItem.getItemId() == R.id.gardens_menu) {
                Intent intentToOpenGardenClass = new Intent(TouristAttractions.this, Garden.class);
                startActivity(intentToOpenGardenClass);
            } else {
                mDrawerLayout.closeDrawers();
            }
            return true;
        }
    });
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout_for_menu_items);
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager_for_menu_items);
    AdapterForFragmentOfTouristAttraction adapterForFragmentOfTouristAttraction = new AdapterForFragmentOfTouristAttraction(this, getSupportFragmentManager());
    viewPager.setAdapter(adapterForFragmentOfTouristAttraction);
    tabLayout.setupWithViewPager(viewPager);
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
    params.setMargins(0, toolbar.getHeight(), 0, 0);
    ActionBar actionbar = getSupportActionBar();
    /*
    actionBar.setDisplayHomeAsUpEnabled(true) will make the icon clickable and add the < at the left of the icon.

     */
    actionbar.setDisplayHomeAsUpEnabled(true);
    /*
    Enabling the menu icon of the navigation.However note we are simply adding the menu icon however clicking on the icon does absolutely nothing.
     */
    actionbar.setHomeAsUpIndicator(R.drawable.baseline_menu_white_24);

}

}

2 个答案:

答案 0 :(得分:1)

首先,最好使用垂直方向的LinearLayout,RelativeLayout或ConstraintLayout将视图彼此定位。在更奇特的用例中,甚至CoordinatorLayout可能也更适合。

FrameLayouts通常用于简单地彼此叠加视图,而彼此之间没有依赖关系。


这就是说,您遇到的问题是因为您在添加视图后立即使用toolbar.getHeight(),该视图将始终为0。尝试附加调试器,亲自看看!

原因是Android需要一些时间来measure -> layout -> draw进行查看,如果您在添加布局后立即调用getHeight,则将不会执行上述任何一个步骤,而不会初始化这些值,位于0

有多种解决方法,但是同样,最好还是使用不同的布局。如果您坚持使用FrameLayout,最干净的方法是扩展它并创建自己的框架,您可以在其中自己测量和布置视图。骇人听闻,难以维护且令人困惑的方法是使用ViewTreeObserver来监听更改并对它们做出反应。这很糟糕,因为您必须等待完整版式通过之后才能触发另一遍。


不要在这里使用FrameLayout。

答案 1 :(得分:0)

你还没做完

yourView.setLayoutParams(params);