相关疑难解决方法(0)

从当前活动中获取根视图

我知道如何使用View.getRootView()获取根视图.我也能从一个按钮的onClick事件中获取视图,其中参数是一个视图.但是如何在活动中获取视图

android view android-activity

604
推荐指数
9
解决办法
50万
查看次数

在运行时确定Android视图的大小

在创建活动后,我正在尝试将动画应用到我的Android应用中的视图.为此,我需要确定视图的当前大小,然后设置动画以从当前大小缩放到新大小.此部分必须在运行时完成,因为视图根据用户的输入缩放到不同的大小.我的布局是用XML定义的.

这似乎是一件容易的事,虽然没有一个能解决我的问题,但很多问题都存在.所以也许我错过了一些明显的东西.我通过以下方式了解了我的观点:

ImageView myView = (ImageView)getWindow().findViewById(R.id.MyViewID);
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但打电话时getWidth(),getHeight(),getMeasuredWidth(),getLayoutParams().width,等,他们都返回0.我也试过手动调用measure()视图上,然后到一个呼叫getMeasuredWidth(),但没有效果.

我已经尝试调用这些方法并在我的activity onCreate()和in中检查调试器中的对象onPostCreate().如何在运行时确定此视图的确切尺寸?

android

113
推荐指数
8
解决办法
14万
查看次数

如何以编程方式获取Android导航栏的高度和宽度?

Android屏幕底部的黑色导航栏无法轻松移除.它从3.0开始就是Android的一部分,作为硬件按钮的替代品.这是一张图片:

系统栏

如何获得此UI元素的宽度和高度的大小(以像素为单位)?

graphics user-interface android android-activity

113
推荐指数
8
解决办法
10万
查看次数

Android透明状态栏和操作栏

我已经对这个主题做了一些研究,我找不到一个完整的解决方案,所以,一步一步,经过一些反复试验,我终于找到了如何实现这些结果:透明或有色ActionbarStatusbar.请看下面的答案.

android android-appcompat android-actionbar android-toolbar android-statusbar

93
推荐指数
1
解决办法
11万
查看次数

OnGlobalLayoutListener:弃用和兼容性

我必须使用一个OnGlobalLayoutListener对象,然后删除监听器,我有一个问题与我用以下代码解决的弃用方法.

protected void onCreate(Bundle savedInstanceState) {
    final LinearLayout llTotal = (LinearLayout) findViewById(R.id.mmc_ll);
    ViewTreeObserver vto = llTotal.getViewTreeObserver();
    if(vto.isAlive()){
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //
                // mycode
                //
                if (Build.VERSION.SDK_INT<16) {
                    removeLayoutListenerPre16(llTotal.getViewTreeObserver(),this);
                } else {
                    removeLayoutListenerPost16(llTotal.getViewTreeObserver(), this);
                }
            } 
        });
    }
    super.onCreate(savedInstanceState);
}

@SuppressWarnings("deprecation")
private void removeLayoutListenerPre16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
    observer.removeGlobalOnLayoutListener(listener);
}

@TargetApi(16)
private void removeLayoutListenerPost16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
    observer.removeOnGlobalLayoutListener(listener);
}
Run Code Online (Sandbox Code Playgroud)

这是对的吗?有更好的方法来处理兼容性?

使用API​​ 10在模拟器中运行代码我在LogCat中有以下警告

04-24 09:30:12.565: I/dalvikvm(471): Could not find method android.view.ViewTreeObserver.removeOnGlobalLayoutListener, referenced from method com.my.project.ActivityHome.removeLayoutListenerPost16
04-24 …
Run Code Online (Sandbox Code Playgroud)

compatibility android deprecated

72
推荐指数
5
解决办法
4万
查看次数

状态栏的高度?

有没有办法获得状态栏+标题栏的高度?检查开发论坛显示相同的问题,但没有解决方案(我可以找到).

我知道我们可以在初始布局通过后得到它,但我希望在我的活动的onCreate()中得到它.

谢谢

android

65
推荐指数
7
解决办法
8万
查看次数

显示指标减去状态栏?

我需要获取应用程序可以在设备上使用的显示矩形的尺寸.为此,我尝试使用:

Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Run Code Online (Sandbox Code Playgroud)

我的问题是它给了我整个显示器的高度,并且显示器顶部有一个"状态/通知"栏,应用程序无法使用.

我需要应用程序可以使用的实际维度.

为了帮助您更好地理解这个问题,我将留下一个图片:

在此输入图像描述

android

31
推荐指数
2
解决办法
1万
查看次数

如何获取状态栏和软键按钮栏的高度?

在Android中是否有办法获得软按钮栏和状态栏的大小?或者一种方法去获得没有这两个高度的屏幕高度?(它们并不总是一样,例如看nexus 7)

android softkeys statusbar

17
推荐指数
2
解决办法
3万
查看次数

没有状态栏,actionBar和标签的屏幕高度

我有一个ListView,我希望每行填充可用屏幕的三分之一.我可以看到状态栏,然后是一个带有slidingTabs的actionBar.我正在做这样的当前计算:

height = context.getResources().getDisplayMetrics().heightPixels;
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
    {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,context.getResources().getDisplayMetrics());
        Log.d("actionBarHeigth", actionBarHeight.toString());
    }
Run Code Online (Sandbox Code Playgroud)

并设置视图高度如下:

holder.imageView.getLayoutParams().height = (height - actionBarHeight*2) / 3;
Run Code Online (Sandbox Code Playgroud)

但是列表的行有点太大了,我想这是导致它的状态栏.如何将其高度添加到我的计算中?

android android-actionbar

14
推荐指数
2
解决办法
1万
查看次数

在Android中将状态栏填充设置为NavigationView

我有一个活动,它从支持库托管DrawerLayout和NavigationView.我正在为导航视图设置标题布局,我希望导航标题高度为"wrap_content".因此,当我将高度设置为"wrap_content"时,标题布局会位于状态栏后面.

我想要的结果是导航抽屉应该在状态栏后面绘制,但导航标题应该按状态栏高度向下推.

下面是我得到的截图.请注意状态栏后面的"SIGN IN"按钮.

截图

活动布局

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nav_drawer"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager>
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <include layout="@layout/include_toolbar"/>

        <android.support.design.widget.TabLayout
            app:theme="@style/ThemeOverlay.AppCompat.Dark"
            style="@style/MyCustomTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tabs"
            />

    </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/menu_navigation"
    android:fitsSystemWindows="true"
    android:layout_gravity="start"/>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

导航视图标题布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="?attr/colorPrimaryDark"
          android:padding="16dp"
          android:theme="@style/ThemeOverlay.AppCompat.Dark"
          android:orientation="vertical"
          android:fitsSystemWindows="true"
          android:gravity="bottom">

<TextView
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text_user_name"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

<TextView
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text_email"
    android:textAppearance="@style/TextAppearance.AppCompat.Body2"/>

<Button
    android:id="@+id/button_sign_in"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sign In"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我通过StackOverflow搜索了一个解决方案,但找不到它.所以有人请说清楚.提前致谢.

android navigation-drawer material-design navigationview androiddesignsupport

13
推荐指数
2
解决办法
7505
查看次数