标签: android-layoutparams

Android:ViewGroup.MarginLayoutParams 不起作用

我用它来以编程方式设置边距,但它不起作用,不应用边距。在构造函数中:

public TimeWindow(Context context, int pixels, int left, int top){
    super(context);
    ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(pixels, pixels);
    params.setMargins(left, top, 0, 0);
    this.setLayoutParams(params);
}
Run Code Online (Sandbox Code Playgroud)

android android-layoutparams

5
推荐指数
1
解决办法
6430
查看次数

Picasso .fit().centerCrop()不使用WRAP_CONTENT

我有一个ImageView以编程方式创建的,并使用Picasso加载图像.ImageView的高度和宽度设置为WRAP_CONTENT.图像非常大,所以我想使用Picasso的.fit().centerCrop()方法来节省内存.但是,当我添加方法时,图像不会显示,我认为它必须是因为WRAP_CONTENT.这是我的代码:

ImageView iv = new ImageView(getContext());
RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(centerParams);
Picasso.with(context)load(path).fit().centerCrop().into(iv); //if I remove .fit().centerCrop(), the images load just fine
Run Code Online (Sandbox Code Playgroud)

我能找到的唯一信息是GitHub关于它的这个问题 - 虽然问题说它在v2.4中已经关闭,但有些人评论说他们在v2.5中遇到过它.(我也使用v2.5)

编辑:根据Angela的建议,这就是我现在使用的内容:

ImageView iv = new ImageView(getContext());
RelativeLayout.LayoutParams centerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(350));
centerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
iv.setLayoutParams(centerParams);
iv.setAdjustViewBounds(true);                                    
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(getContext()).load(path).fit().into(iv);
Run Code Online (Sandbox Code Playgroud)

现在显示图像,但它们没有正确缩放,宽高比会以某种方式改变.关于如何保持纵横比同时仍允许毕加索进行测量的任何想法.fit()

android android-image android-imageview picasso android-layoutparams

5
推荐指数
1
解决办法
4594
查看次数

水平 android:animateLayoutChanges="true" 动画不流畅

我有一个动画布局,如下所示

在此输入图像描述

txt_billion使用android:animateLayoutChanges="true"(下面的布局代码)动态显示。

注意Hundred是跳跃的(实际上所有的都在跳跃,只是Hundred更明显)。如何防止文字跳动?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#9f9"
        android:text="Hundreds" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#f9f"
        android:text="Thousands" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:background="#0ff"
        android:text="Millions" />
    <TextView
        android:id="@+id/txt_billion"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:padding="8dp"
        android:visibility="gone"
        android:background="#ff0"
        android:text="Billions" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

您可以从https://github.com/elye/issue_horizo ​​ntal_layout_animate 获取代码进行测试

android android-animation android-layout android-layout-weight android-layoutparams

5
推荐指数
1
解决办法
2074
查看次数

如何以编程方式访问ConstraintLayout中视图的当前约束?

我可以使用ConstraintSet在ConstraintLayout中以编程方式修改视图的约束,但是在应用修改之前,我想测试它是否尚未完成。为此,我需要以编程方式访问ConstraintLayout中视图的当前约束。

例如,在这里我想删除TextView“标题”的顶部和TextView视图“ subtitle”的底部之间的约束。然后将TextView“标题”的顶部约束到“ videoView”的底部。但是首先,我需要检查TextView的“标题”是否尚未限制在“ videoView”的底部(从android的官方网站修改的代码)。请注意,我无法在此网站上插入HTML的最后一行,在尖括号中为/android.support.constraint.ConstraintLayout。

public class MainActivity extends AppCompatActivity {
ConstraintSet mConstraintSet = new ConstraintSet(); // create a Constraint Set
ConstraintLayout mConstraintLayout; // cache the ConstraintLayout

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mConstraintLayout = (ConstraintLayout) findViewById(R.id.root);
    mConstraintSet.clone(mConstraintLayout); // get constraints from ConstraintSet
}

public void userDoesSomething(View v) {
    //test if title has already been constrained to bottom of videoView
    if (mConstraintLayout 'TITLE' IS NOT CONSTRAINED TO THE BOTTOM OF 'VIDEOVIEW'){
        mConstraintSet.connect(R.id.Title, ConstraintSet.TOP, R.id.videoView, ConstraintSet.BOTTOM); // …
Run Code Online (Sandbox Code Playgroud)

android android-layoutparams android-constraintlayout

5
推荐指数
1
解决办法
3858
查看次数

将始终可见的聊天头的LayoutParams更改为不总是可见

我试图添加一个我通过服务生成的视图.我使用的代码基于Facebook Chatheads,无论应用程序状态如何,它们始终可见.它们也显示在其他任何东西上面:

在此输入图像描述 Android聊天头

我现在希望将聊天头限制在活动应用程序中.具体来说,每当我将Window.LayoutParams从TYPE_PHONE更改为TYPE_DRAWN_APPLICATION时,我正在处理Bad Token异常.

我的问题:我知道我需要将正确的窗口令牌传递给LayoutParams,但似乎无法弄清楚如何正确执行此操作.任何建议都将受到高度赞赏.

这是我的代码:

//主要活动

private void addNewBubble() {
        BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
        bubblesManager.addBubble(bubbleView, 60, 20);
}

// initializes Bubbles Manager
private void initializeBubblesManager() {
        bubblesManager = new BubblesManager.Builder(this)
                .setTrashLayout(R.layout.task_bubble_trash_layout)
                .setInitializationCallback(new OnInitializedCallback() {
                    @Override
                    public void onInitialized() {
                        addNewBubble(); // Called when addNewBubble is initialized and the bubble data is loaded. When used on devices running API 18 or below, this function is always called.

                    }
                })
                .build();
        bubblesManager.initialize();
    }

// initializes Bubbles Manager
        private …
Run Code Online (Sandbox Code Playgroud)

service layout android token android-layoutparams

5
推荐指数
1
解决办法
332
查看次数

从自定义布局中的子视图读取自定义布局属性

我创建了自己的布局。我定义了一个“好”属性用于其子视图。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyLayout">
        <attr name="good" format="boolean" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

该属性是这样使用的。它有点像您可以用于android:layout_centerInParenta 的子视图RelativeLayout,尽管我不确定为什么我的视图应该以“app:”开头,而以“android:”开头。

<?xml version="1.0" encoding="utf-8"?>
<com.loser.mylayouttest.MyLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Button
        app:good = "true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</com.loser.mylayouttest.MyLayout>
Run Code Online (Sandbox Code Playgroud)

现在我想从孩子们那里读到这个属性。但如何呢?我在网上搜索并尝试了一些方法,但似乎不起作用。

class MyLayout: LinearLayout
{
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)
    {
        setMeasuredDimension(200, 300); // dummy

        for(index in 0 until childCount)
        {
            val child = getChildAt(index);
            val aa = child.context.theme.obtainStyledAttributes(R.styleable.MyLayout);
            val good = aa.getBoolean(R.styleable.MyLayout_good, false)
            aa.recycle();
            Log.d("so", "value = $good")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

补充:通过评论作为提示,我找到了这个文档,并修改了我的代码,如下所示,现在我得到了我想要的结果。 …

android android-layout android-layoutparams

5
推荐指数
0
解决办法
784
查看次数

AS找不到BottomSheetBehavior_Params,_behavior_peekHeight和_behavior_hideable

我只是将我的应用程序更新为支持库版本24.0.0,我收到一些关于BottomSheet Params的错误.
代码:

/**
 * Default constructor for inflating BottomSheetBehaviors from layout.
 *
 * @param context The {@link Context}.
 * @param attrs   The {@link AttributeSet}.
 */
public BottomSheetBehaviorGoogleMapsLike(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.obtainStyledAttributes(attrs,
            android.support.design.R.styleable.BottomSheetBehavior_Params);
    setPeekHeight(a.getDimensionPixelSize(
            android.support.design.R.styleable.BottomSheetBehavior_Params_behavior_peekHeight, 0));
    setHideable(a.getBoolean(android.support.design.R.styleable.BottomSheetBehavior_Params_behavior_hideable, false));
    a.recycle();
    ViewConfiguration configuration = ViewConfiguration.get(context);
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
Run Code Online (Sandbox Code Playgroud)

Android Studio找不到:

  • android.support.design.R.styleable.BottomSheetBehavior_Params
  • android.support.design.R.styleable.BottomSheetBehavior_Params_behavior_peekHeight
  • android.support.design.R.styleable.BottomSheetBehavior_Params_behavior_hideable

知道他们搬到哪里了吗?

android android-support-library android-studio android-layoutparams bottom-sheet

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

窗口类型 2038 的权限被拒绝

我正在修复一个不是我开发的 android 应用程序,几天前客户端要求我将 targetSdkVersion 设置为 26(之前是 14),之后我注意到应用程序在某些情况下崩溃,例如崩溃时显示 ProgressDialog。这是存在此问题的代码部分:

mProgressDialog.setTitle(getString(R.string.downloading_data));
mProgressDialog.setMessage(mAlertMsg);
Drawable icon = getActivity().getDrawable(android.R.drawable.ic_dialog_info);
icon.setColorFilter(Color.parseColor("#00cbac"), PorterDuff.Mode.SRC_ATOP);
mProgressDialog.setIcon(icon);
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.setButton(getString(R.string.cancel), loadingButtonListener);
mProgressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mProgressDialog.show();
Run Code Online (Sandbox Code Playgroud)

执行最后一行时,应用程序崩溃并在 Logcat 中出现此错误:

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b784d3a -- permission denied for window type 2003
Run Code Online (Sandbox Code Playgroud)

当应用程序运行在具有Android 8和9的设备上时会出现此问题。我寻找了类似问题的解决方案,发现使用WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY代替会更好TYPE_SYSTEM_ALERT,然后我修改了我写的代码的倒数第二行在这篇文章中,但它没有改变任何东西,应用程序崩溃了,并且在日志中出现了这个错误:

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b784d3a -- permission denied for window type 2038
Run Code Online (Sandbox Code Playgroud)

在清单中,我有权限:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Run Code Online (Sandbox Code Playgroud)

我还从设备设置中启用了该应用程序的所有权限。我怎么解决这个问题?

android android-windowmanager android-layoutparams

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

UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams)

我正在尝试创建一个简单的Android应用程序,我收到此错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twista.shopinglist/com.twista.shopinglist.ItemDetail}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
Run Code Online (Sandbox Code Playgroud)

我尝试谷歌它并通过这里的一些答案修复,但不成功它没有解决:(.

我有一个MainActivityListViewonCreate()我打电话

 public class MainActivity extends Activity {

 ....

 @Override
 protected void onCreate(Bundle savedInstanceState) {

    ...

    final ListView listView = (ListView) findViewById(R.id.overListView);

    List<Item> values = dataSource.getItems();

    final ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this,
            android.R.layout.simple_list_item_1, values);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Item i = adapter.getItem(position);
            Intent intent = new Intent(MainActivity.this, …
Run Code Online (Sandbox Code Playgroud)

android list view android-activity android-layoutparams

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

如何以编程方式创建两个等宽的 LinearLayout?

我正在尝试以LinearLayout编程方式创建两个等宽: 在此处输入图片说明

mGroupLayout.setOrientation(HORIZONTAL);
mGroupLayout.setWeightSum(2f);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        0,
        LinearLayout.LayoutParams.WRAP_CONTENT
        );
params.weight = 1.0f;


leftLayout = new LinearLayout(getContext());
leftLayout.setOrientation(VERTICAL);
leftLayout.setLayoutParams(params);


mGroupLayout.addView(
        leftLayout,
        params
        );

rightLayout = new LinearLayout(getContext());
rightLayout.setOrientation(VERTICAL);
rightLayout.setLayoutParams(params);

mGroupLayout.addView(
        rightLayout,
        params
        );
Run Code Online (Sandbox Code Playgroud)

但是我所有的线性布局都不可见(它们的宽度为 0)。我怎么能这样做?

android android-linearlayout android-layoutparams

0
推荐指数
1
解决办法
649
查看次数

线性布局匹配父级或权重在协调器布局中不起作用

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.now.sexyfacechanger.Activity_PhotoOrGallery">

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_camera"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_camera"
            android:layout_weight="1"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_gallery"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_weight="1"/>

    </LinearLayout>


</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

这是android studio设计中的视图,显示的线性布局与父级匹配:

在此处输入图片说明

但是输出:

在此处输入图片说明

线性布局与父布局不匹配,或者我怀疑应用于两个浮动按钮的 layout_weight 在协调器布局中不起作用。

android android-linearlayout android-layout-weight android-coordinatorlayout android-layoutparams

0
推荐指数
1
解决办法
1904
查看次数