工具栏未在创建或XML中设置高程

时间:2018-08-21 09:25:30

标签: android xml android-toolbar

我目前正在为学校创建一个Android应用程序,但仍想尽我所能。总的来说,我对Android开发和编码还很陌生。 该应用程序应该是股票市场游戏。 (顺便说一句,我是德语,所以可能会有一些德语变量) 该应用应该具有与Google最近的Pixel 2和Android Pie相似的风格。因此,工具栏在创建时应该没有阴影,但在滚动时应该出现,就像在Pixel 2的设置应用程序中(例如“电池”标签)一样。 阴影在滚动时出现,到达顶部时消失,但是工具栏以阴影开始,尽管我在XML中将android:elevation设置为0,在{{1}中将toolbar.setElevation(0)设置为} 方法。 为什么会这样?此方法适用于onCreate

Java代码:

OnScrollChangedListener

XML代码:

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<JSONObject> jObjList = new ArrayList<>();
    private FloatingActionButton fab;
    private TextView moneyTxt;
    private TextView sharesTxt;
    private TextView sumTxt;
    private Toolbar toolbar;
    private AppBarLayout toolbarLayout;
    private RecyclerView recyclerShares;
    private SharesAdapter sAdapter;
    private NestedScrollView scrollMain;
    private SwipeRefreshLayout refreshMain;
    private float money;
    private float sharesWorth;
    private boolean isRefreshing;
    private JSONObject jObj = new JSONObject();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.abMain);
        toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimary));
        setSupportActionBar(toolbar);

        fab = findViewById(R.id.fabMain);
        moneyTxt = findViewById(R.id.moneyTxt);
        sharesTxt = findViewById(R.id.sharesTxt);
        sumTxt = findViewById(R.id.sumTxt);
        toolbarLayout = findViewById(R.id.abMainLayout);
        recyclerShares = findViewById(R.id.recyclerShares);
        scrollMain = findViewById(R.id.scrollMain);
        scrollMain.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
        refreshMain = findViewById(R.id.refreshMain);
        isRefreshing = false;

        try {
            jObj.put("name", "BMW");
            jObj.put("worth", 143.23);
            jObj.put("count", 5);
            jObj.put("change", -1.5);

            jObjList.add(jObj);
        } catch (JSONException e) {
        }

        sAdapter = new SharesAdapter(jObjList);
        RecyclerView.LayoutManager cLayoutManager = new CustomGridLayoutManager(getApplicationContext()) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerShares.setLayoutManager(cLayoutManager);
        recyclerShares.setItemAnimator(new DefaultItemAnimator());
        recyclerShares.setAdapter(sAdapter);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, SharesActivity.class);
                startActivity(intent);
            }
        });

        toolbarLayout.setElevation(0); //TODO: Stackoverflow nach Lösung fragen
        scrollMain.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                int scroll = scrollMain.getScrollY();
                if (scroll == 0) {
                    toolbarLayout.setElevation(0);
                } else {
                    toolbarLayout.setElevation(8);
                }
            }
        });

        refreshMain.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh(MainActivity.this);
            }
        });

        refresh(MainActivity.this);
    }

    private void refresh(Context context) {
        isRefreshing = true;

        SharedPreferences sharedPref = context.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
        SharedPreferences.Editor sharedPrefEdit = sharedPref.edit();

        if (sharedPref.getBoolean("isFirstRun", true)) {
            sharedPrefEdit.putBoolean("isFirstRun", false);
            sharedPrefEdit.putFloat(getString(R.string.moneyShared), 5000);
            sharedPrefEdit.apply();
        }

        float shareWorth = 0;

        for (int i = 0; i < jObjList.size(); i++) {
            try {
                shareWorth += jObjList.get(i).getDouble("worth") * jObjList.get(i).getDouble("count");
            } catch (JSONException e) {
            }
        }
        sharedPrefEdit.putFloat(getString(R.string.sharesWorthShared), shareWorth);
        sharedPrefEdit.commit();

        money = sharedPref.getFloat(getString(R.string.moneyShared), 0);
        sharesWorth = sharedPref.getFloat(getString(R.string.sharesWorthShared), 0);

        moneyTxt.setText(String.format("%.2f€", money));
        sharesTxt.setText(String.format("%.2f€", sharesWorth));
        sumTxt.setText(String.format("%.2f€", money + sharesWorth));

        if(isRefreshing) {
            isRefreshing = false;
            refreshMain.setRefreshing(isRefreshing);
        }

        Toast.makeText(MainActivity.this, "Alles neugeladen", Toast.LENGTH_SHORT).show();
    }

    public void onShareClick(View v) {
        Intent i = new Intent(MainActivity.this, CompanyActivity.class);
        try {
            i.putExtra("name", jObj.getString("name"));
            i.putExtra("worth", jObj.getDouble("worth"));
        } catch (JSONException e) {
            i.putExtra("name", "Fehler");
            i.putExtra("worth", 0);
        }
        startActivity(i);
    }
}

2 个答案:

答案 0 :(得分:2)

所以 @NileshRathod 回答了:

它是 app:elevation ,而不是 android:elevation 。 但是我仍然不明白为什么onCreate()中的方法不起作用。

答案 1 :(得分:1)

当然,setElevation(0);不起作用。

  

但是我仍然不明白为什么onCreate()中的方法不起作用。

AppbarLayout使用v 24.0.0中的StateListAnimator,这就是setElevation对它不起作用的原因:https://stackoverflow.com/a/37992366/4409113

所以:

StateListAnimator stateListAnimator = new StateListAnimator();
stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));
appBarLayout.setStateListAnimator(stateListAnimator);

或者:

toolbarLayout = findViewById(R.id.abMainLayout); 
    toolbarLayout.StateListAnimator = null;

以您为例。