相关疑难解决方法(0)

避免将null作为视图根传递(需要解析膨胀布局的根元素上的布局参数)

为root studio传递null给了我这个警告:

避免将null作为视图根传递(需要解析膨胀布局的根元素上的布局参数)

它显示为空值getGroupView.请帮忙.

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        super();
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

android warnings android-layout layout-inflater

218
推荐指数
6
解决办法
8万
查看次数

LayoutInflater attachToRoot参数是什么意思?

LayoutInflater.inflate文档是不看好的目的十分清楚我的attachToRoot参数.

attachToRoot:膨胀的层次结构是否应附加到根参数?如果为false,则root仅用于为XML中的根视图创建LayoutParams的正确子类.

有人可以更详细地解释,特别是根视图是什么,并且可能显示一个行为truefalse值之间的变化的例子?

android android-layout layout-inflater android-view

169
推荐指数
9
解决办法
5万
查看次数

Android Fragment不会将match_parent视为高度

对不起,巨大的代码转储,但我真的迷路了.

MyActivity.java onCreate:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_singlepane_empty);
mFragment = new PlacesFragment();
getSupportFragmentManager().beginTransaction()
                    .add(R.id.root_container, mFragment)
                    .commit();
Run Code Online (Sandbox Code Playgroud)

PlacesFragment.java onCreateView:

mRootView = (ViewGroup) inflater.inflate(R.layout.list_content, null);
return mRootView;
Run Code Online (Sandbox Code Playgroud)

注意:mRootView是一个ViewGroup全局,我相信没问题.PlacesFragment是一个ListFragment.

布局:

activity_singlepane_empty.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00f">
    <include layout="@layout/actionbar"/>

    <!-- FRAGMENTS COME HERE! See match_parent above -->

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

list_content.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listContainer"
        android:background="#990"
        >

        <ListView android:id="@android:id/list"
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:drawSelectorOnTop="false" />

        <TextView android:id="@id/android:empty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium" 
                android:text="@string/no_data_suggest_connection"/>

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

问题:正如您所看到的,预期的行为是将上面的空TextView显示在屏幕中心.在Eclipse的设计预览中,没关系.只有当作为片段添加到root_view时,FrameLayout才会填满整个屏幕.

root_container为蓝色,FrameLayout为黄色,请参阅下面的调试目的.黄色窗格不应该填满整个屏幕吗?!?!?!?

在此输入图像描述

android center fragment android-linearlayout android-framelayout

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

设置ListView项目高度

这应该是一件非常容易的事情,但它看起来非常棘手.在我的代码中我有这个ListView:

<ListView android:id="@+id/sums_list" android:layout_width="fill_parent"
        android:layout_height="0dp" android:layout_weight="1" android:dividerHeight="0dp"></ListView>
Run Code Online (Sandbox Code Playgroud)

这是使用使用此视图的ArrayAdapter填充的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cdpb="http://schemas.android.com/apk/res/ale.android.brainfitness"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="55dp">
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

为什么55dp项目不高?

谢谢大家

android listview

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

如何使用layoutinflator在运行时添加视图?

我有两个布局:main.xml和buttonpanel.xml.在buttonpanel.xml中,在main linearlayout中,我将gravity设置为bottom.现在我尝试使用以下代码添加按钮面板布局.

setContentView(R.layout.main);
LinearLayout layout=(LinearLayout)findViewById(R.id.mainlinearlayout);
LayoutInflater inflater= (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.buttonpanel,null);
layout.addView(view);
Run Code Online (Sandbox Code Playgroud)

我的问题是面板被添加到顶部,虽然我已经在buttonpanel.xml中将重力设置为底部.如果我使用include将buttonpanel.xml添加到main.xml,它可以正常工作.

任何人都可以帮我解决我的代码有什么问题吗?

android android-layout

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

RecyclerView项目不填充宽度

我设计了一个带有地图片段和recyclerView的布局.每个recyclerView项都是cardview(我已指定给出xml布局).

问题是RecyclerView项目没有填充屏幕宽度. img在这里

我试图将layout_width更改为fill_parent,match_parent但它无能为力

这是每个项目的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:id="@+id/cardView">

    <LinearLayout
        android:id="@+id/locationItemView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="120px"
            android:layout_height="120px"
            android:id="@+id/imgIcon"
            android:background="@drawable/image_bg"
            android:layout_margin="5dp"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

        <LinearLayout
            android:id="@+id/layoutInfo"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Location Name"
                android:textColor="#d5c645"
                android:textStyle="bold"
                android:textSize="20dp"
                android:padding="3dp" />

            <TextView
                android:id="@+id/txtAddress"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Location Address"
                android:textSize="16dp"
                android:padding="3dp" />

            <TextView
                android:id="@+id/txtDistance"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:text="Location Distance"
                android:textSize="14dp"
                android:textStyle="italic"
                android:padding="2dp"
                android:textAlignment="viewEnd" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

main_layout

<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" …
Run Code Online (Sandbox Code Playgroud)

android android-cardview android-recyclerview

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

Android:是否可以制作视图的副本?

我正在处理的应用程序需要一个动态布局,显示配置文件的Horizo​​ntalScrollView.配置文件只是一个带有图片和一些文本的RelativeLayout.由于我从数据文件中获取数据,因此我需要为每个配置文件创建一个相对布局.首先,我在for循环中以编程方式创建了每个RelativeLayout,然后将其添加到父视图中.这有效,但我不想这样做.我想根据设备的屏幕尺寸等使用不同的布局文件.

然后我想.好吧,如果我的布局上只有一个配置文件怎么办?我的代码可以获得一个配置文件,findViewById()然后根据它创建新的配置文件!换一种说法:

    // get the layout
    profileLayout = (LinearLayout) findViewById(R.id.profileLayout);        

    // get the first profile in the layout
    originalProfile = (RelativeLayout) findViewById(R.id.profile1);

    // make copy of profile
    temporaryProfile = originalProfile;

    // make changes to the this profile and add it back
    profileLayout.addView(temporaryProfile);
Run Code Online (Sandbox Code Playgroud)

当然,这不起作用,因为这是java,而temporaryProfile现在是对originalProfile的引用.那么有没有办法复制这个RelativeLayout?我知道LayoutInflater,但我仍然不明白它是如何工作的.还有Object.clone().

android android-layout

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

自定义样式AlertDialog的样式属性

我试图设置我的AlertDialog样式,我已经能够通过样式和xml声明更改大部分内容......但仍然存在一些问题:

  1. 如何将标题栏周围的区域从黑色更改为自定义颜色?
  2. 如何将外部背景更改为透明(阴影所在的外部部分为蓝色)
  3. 如何更改按钮以使它们不与警报消息周围的黑色边框重叠?

AlertDialog

这是我在RootActivity中的功能(我的活动扩展了这个)

public static void showNoConnectionDialog(Context ctx1) {
    final Context ctx = ctx1;
    LayoutInflater factory = LayoutInflater.from(ctx);
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(ctx, R.style.SetdartDialog));
    builder.setView(factory.inflate(R.layout.alert_dialog, null))
    .setIcon(R.drawable.icon)
    .setCancelable(true)
    .setMessage(R.string.check_wireless_settings)
    .setTitle(R.string.no_connection)
    .setPositiveButton(R.string.myes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
        }
    })
    .setNegativeButton(R.string.mno, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            return;
        }
    })
    .setOnCancelListener(new DialogInterface.OnCancelListener() {
        public void onCancel(DialogInterface dialog) {
            return;
        }
    })
    .show();
}
Run Code Online (Sandbox Code Playgroud)

这里是styles.xml的一个片段

<?xml version="1.0" encoding="utf-8"?>
<resources> …
Run Code Online (Sandbox Code Playgroud)

android styles dialog android-layout android-alertdialog

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

布局宽度和高度被忽略

我有一个内部布局和SurfaceTexture视频流的对话框.当我收到视频widthheight视频时,我会调整我的布局:

private void resizeView(final VideoFormatInfo info) {
        final Size size = calculateSize(info.getWidth(), info.getHeight());
        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {

                final ViewGroup.LayoutParams layoutParams = mInnerLayout.getLayoutParams();
                layoutParams.width = size.x;
                layoutParams.height = size.y;
                Log.i(TAG, String.format("run: setting innerlayout bounds to %d,%d", size.x, size.y));
                mInnerLayout.setLayoutParams(layoutParams);
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

现在我有一个全屏按钮,应该将布局调整到整个屏幕.但是当我按下它时,布局仍然在屏幕的一个小区域内.

当我检查日志中正确的值size.xsize.y是否(屏幕的边界),但布局没有正确调整大小.

innerlayout被添加到名为"VideoPlayer"的customView中.我将视频播放器背景的颜色设置为红色,因此当我切换到全屏时,整个屏幕变为红色,除了中间的视频流.这意味着底层视图正在适当调整大小,但innerLayout不是出于某种原因.

有趣的是,我在视频渲染上有另一种布局,可以创建"闪光效果"来模拟拍摄快照时的相机闪光.触发该闪光效果后,视频将调整为整个屏幕.

所以这是我的布局树:

VideoPlayerView (CustomView, not VideoView)
     innerLayout (RelativeLayout)
         videoSurfaceTexture (SurfaceTextureView)
         flashLayout (RelativeLayout)
Run Code Online (Sandbox Code Playgroud)

我也将其设置为调试:

 @Override
            public void onSurfaceTextureSizeChanged(final SurfaceTexture surfaceTexture, final int width, …
Run Code Online (Sandbox Code Playgroud)

layout android

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

Android:ListView的边距/填充没有应用于标题的边距?

是否可以为ListView设置边距/填充而不将边距应用于标题?我希望我的ListView与Google Now布局一样.除了这个小问题,我已经完成了所有工作.

问题:是否可以不将ListView的布局参数应用于其标题?

EDIT1:更多信息

紫色视图是listView的标题.我已经尝试在列表项布局中添加边距.这也不起作用.我需要的是一种方法,将边距应用于列表视图,以便它不会将边距应用于listView中的标头.

EDIT2:我的布局header.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/top_height"
        android:background="@color/top_item" />

    <View
        android:id="@+id/placeholder"
        android:layout_width="match_parent"
        android:layout_height="@dimen/sticky_height"
        android:layout_gravity="bottom" />

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

root_list.xml

<LinearLayout>
<com.abhijith.CustomListView
    android:id="@android:id/list"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_height="wrap_content" /></LinearLayout>
Run Code Online (Sandbox Code Playgroud)

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/card_background"
android:orientation="vertical" >

<TextView
    android:id="@+id/textview_note"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textview_note_date"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/></LinearLayout>
Run Code Online (Sandbox Code Playgroud)

由于这篇文章的篇幅,我已经删除了uselsess布局属性. 这是它的外观

android android-layout android-listview

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