标签: android-view

为Android应用中的每个视图分别设置首选项

随着应用程序的运行,我有多个视图来来去去.我希望每个视图都有自己的个人首选项,这些首选项存储为视图的ID标记.上面是"常规首选项",子首选项引用它们在创建视图时获取其默认值.

现在我已经设置了General Preferences是默认的SharedPreferences.但我不知道如何创建新的首选项并设置活动UI,以便用户可以更改它们.它与设置SharedPreferences几乎相同吗?

android android-preferences sharedpreferences android-view

4
推荐指数
1
解决办法
2241
查看次数

Android 2.2上的View兼容方法(setScale和setPivot)

我有一个专为Honeycomb设计的应用程序,现在我需要让它在旧的Galaxy Tab(Android 2.2)上运行.

但我使用了一些不属于Froyo API的方法:

        //child is a View
        child.setPivotX(0);
        child.setPivotY(0);
        child.setScaleY(scaleFromOriginal);
        child.setScaleX(scaleFromOriginal);
Run Code Online (Sandbox Code Playgroud)

有人知道如何在2.2上取得相同的结果?

PS:我正在寻找兼容性包,但这些方法没有帮助.

android android-2.2-froyo android-3.0-honeycomb android-view

4
推荐指数
1
解决办法
1230
查看次数

无法删除windowManager.addView()附加的视图

我正在尝试删除添加到WindowManager的图层.但是当我调用removeView()时没有任何反应.有人知道如何删除它吗?我的代码看起来像这样.

public class MainActivity extends Activity implements View.OnClickListener{

    private WindowManager wm;
    private  WindowManager.LayoutParams orientationLayout;
    private LinearLayout orientationChanger;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // init landscape things
        wm = (WindowManager) getApplicationContext().getSystemService(Service.WINDOW_SERVICE);
        orientationChanger = new LinearLayout(getApplicationContext());
        orientationChanger.setClickable(false);
        orientationChanger.setFocusable(false);
        orientationChanger.setFocusableInTouchMode(false);
        orientationChanger.setLongClickable(false);
        orientationLayout = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.RGBA_8888);
        orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

        // set view
        setContentView(R.layout.calibrate);

        findViewById(android.R.id.button).setOnClickListener(this);

        lockLandScape();
    }

    public void lockLandScape(){
        wm.addView(orientationChanger, orientationLayout);

        orientationChanger.setVisibility(View.GONE);
        orientationLayout.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        wm.updateViewLayout(orientationChanger, orientationLayout);
        orientationChanger.setVisibility(View.VISIBLE);
    }

    public void releaseLandScape(){
        wm.removeView(orientationChanger);

        // This …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-view android-windowmanager

4
推荐指数
1
解决办法
8692
查看次数

Android | setOnClickListener和setOnTouchListener有什么区别?

setOnClickListener和setOnTouchListener有什么区别?

就......而言:

编辑:

  1. 性能方面,加载/响应视图更快,并且
  2. 优选的选择,这个类似的问题,非常好地回答了我的第二部分.

谢谢

android android-view

4
推荐指数
1
解决办法
2412
查看次数

SurfaceView和ImageView之间的区别

我想知道SurfaceView和ImageView之间的区别及其使用场景.两者似乎都是一样的.如果有可能错过的链接,请指导我.

android android-view

4
推荐指数
1
解决办法
3929
查看次数

按下时如何将图像部分灰显?

我有一个用作按钮的图像.它是RelativeLayout中的ImageView.RelativeLayout是接收触摸事件的视图.触摸时,我想将图像部分灰化为反馈.我该怎么做呢?

我能想到的最好的方法是在ImageView上放置另一个带有黑色背景的View,并将其设置为100%透明,但触摸时只有50%透明.有没有办法直接设置RelativeLayout或ImageView的样式而不使用额外的View?

提前致谢...

android android-view android-styles

4
推荐指数
1
解决办法
3916
查看次数

Android自定义视图组委托addView

我希望ViewGroup在我的情况下实现自定义,FrameLayout但我希望从xml添加的所有子视图不是直接添加到此视图中,而是FrameLayout包含在此自定义中ViewGroup.让我举例说明一下.

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical">
    <FrameLayout
        android:id="@+id/frame_layout_child_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <FrameLayout
        android:id="@+id/frame_layout_top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</merge>
Run Code Online (Sandbox Code Playgroud)

我想重定向将所有子视图添加到FrameLayoutid frame_layout_child_container.

所以当然我推翻方法addView()是这样

  @Override
    public void addView(View child) {
        this.mFrameLayoutChildViewsContainer.addView(child);
    }
Run Code Online (Sandbox Code Playgroud)

但肯定这不起作用,因为此时mFrameLayoutChildViewsContainer不会添加到根自定义视图.

我的想法总是在这个容器的顶部保留一些视图,frame_layout_top并且所有添加到自定义组件中的子视图都应该去frame_layout_child_container

使用自定义视图的示例

   <CustomFrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"/>
    </CustomFrameLayout>
Run Code Online (Sandbox Code Playgroud)

所以在这种情况下TextView应该添加到frame_layout_child_container

是否可以委托ViewGroup像我描述的那样将所有视图添加到子项中.

我有其他的想法,比如bringToFront()每次添加视图时使用方法以保持正确的z轴顺序,或者例如添加视图时,将其保存到数组中,然后在充气自定义视图后将所有视图添加到此子项FrameLayout

建议在这种情况下做什么,以便在每次添加新视图时都不会通过重新填充所有布局来达到性能,如果可以以其他方式实现的话.

performance android android-custom-view android-layout android-view

4
推荐指数
1
解决办法
2424
查看次数

Android:在recyclerView中更改视图的可见性

recyclerView在我的项目中实现了一个.

我有一个Button在我的recyclerView row.我的每一行代码recyclerView都是这样的:

savedmessage_custom_row.xml

<?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="match_parent"
android:layout_margin="4dp"
android:background="#ffffff"
android:orientation="vertical">
<TextView
    android:id="@+id/message"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:text="Dummy text" />
<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:visibility="gone"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

按钮的可见性是gone.当有人点击message textView上面的按钮时,我想将此按钮的可见性更改为"可见" .我实现了一个简单onClickLiestener()message (textView)改变了button点击的可见性message.我知道那不行,但我想看看结果.结果很奇怪.如果我单击第1行的textView,第7,17,19行等按钮也会变得可见.我猜这可能是对viewHolder的缓存.

MyViewHolder是这样的:

class MyViewHolder extends RecyclerView.ViewHolder {
    TextView message;
    public MyViewHolder(final View itemView) {
        super(itemView);
        message = (TextView) itemView.findViewById(R.id.message);
        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View …
Run Code Online (Sandbox Code Playgroud)

android android-view android-viewholder android-recyclerview

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

我是否需要在onDestroy期间从视图中释放View.OnClickListener?

我有一个包含100个碎片的大型项目.他们几乎都遵循我将描述的模式,我的问题将随之而来.

public class MyFragment extends Fragment implements View.OnClickListener {

    public void onDestroy(...) {
    }

    public void onCreateView(...) {
        ...
        root.findViewById(R.id.some_button).setOnClickListener(this);
        return root;
    }

    public void onClick(View v) { ... }
}
Run Code Online (Sandbox Code Playgroud)

我是否需要从片段中释放侦听器onDestroy?这是内存泄漏还是会导致任何问题?清理它们是否被视为良好做法?如果可能的话,解释原因?

android android-view

4
推荐指数
1
解决办法
254
查看次数

在嵌套的RecyclerView中保存滚动状态

我的申请中有问题.我有主要清单(回收者视图).此列表保留20个水平回收站视图.它看起来非常类似于Netflix应用程序.我需要在主列表中保留每个"轮播"的状态.如果我将第一个项目向右移动,向左移动,向下滚动(第一个和第二个项目被回收),旋转屏幕,向上滚动,我需要恢复第一个和第二个元素的位置.

我知道我应该使用linearLayout.saveOnInstanceState和onRestoreState但我的问题是每个轮播都在主列表中.

configChanges 在AndroidManifest.xml中无法发挥作用......

谢谢!

android android-layout android-scrollview android-view android-recyclerview

4
推荐指数
2
解决办法
2418
查看次数