【问题标题】:Resetting CardView's Elevation to the default value (resting elavation)将 CardView 高程重置为默认值(静止高程)
【发布时间】:2015-08-29 15:31:12
【问题描述】:

我想更改CardView 的高度并稍后重新设置。

目前,我将海拔的默认值(称为静息海拔)保留为onCreateViewFragment 的字段值。稍后当我想重置时,将值设置为setCardElevation

private CardView cardView;
private float restingElevation;
private float pickedUpState = 50.0f;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment, container, false);

    cardView = (CardView) view.findViewById(R.id.table_top);
    restingElevation = cardView.getCardElevation(); // keep the default
    Log.d("elevation", "resting: " + restingElevation);

    return view;
}

private void pickUp() {
    cardView.setCardElevation(pickedUpState);
}

private void rest() {
    cardView.setCardElevation(restingElevation);
}

但是,我想知道是否有任何直接的方法可以将CardView 的标高设置为其默认值(静止标高),而不保留onCreateView 中尚未更改的值。 Material Design 主题的android.support.v7.cardview:cardElevation 是否有资源参数(或样式)?

(用LogCat看来CardView的静止高程大约是2.66。实际上在官方Material Design Guide中,Card的静止高程是在2dp和3dp之间标明的。)

【问题讨论】:

    标签: android android-resources android-theme android-styles android-cardview


    【解决方案1】:

    我已设法找到CardView 的默认高度(静止高度)。

    float elevation = getResources().getDimension(R.dimen.cardview_default_elevation);
    Log.d("elavation", "this is the default: " + elevation);
    

    那是R.dimen.cardview_default_elevation

    附加检查:

    其实R.dimen.cardview_default_elevation只是2dp,在官方Material Design Guide中有描述。而 getDimension 返回的值以 px 为单位,具体取决于设备。所以我的设备是 Nexus7 2012,它的屏幕密度为 tvdpi(mdpi 为 x1.33),返回值计算为2dp x 1.33 = 2.66px

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2017-07-12
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多