我浏览了一些教程,在Android Doc中,它说在实例化时不直接访问LayoutInflater.来自Google Doc的示例:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)
我经历的教程就是这个:
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Run Code Online (Sandbox Code Playgroud)
所以除了显而易见的不同代码之外,我真正理解的不同之处在于.任何解释都非常感激.我假设Android Doc应该是我们遵循的那个,但我不确定它是否有所作为.
我有一个视图的XML定义,我将使用addChild添加到更大的容器视图中.它基于a LinearLayout
,看起来基本上是这样的:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="52dip"
android:background="@drawable/box_bg"
android:clickable="true"
android:onClick="onTreeBoxClick"
android:orientation="horizontal" >
<ImageView android:id="@+id/box_photo"
android:layout_width="45dip"
android:layout_height="45dip"
...
/>
<RelativeLayout
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
>
Run Code Online (Sandbox Code Playgroud)
(省略剩余 - 可能不相关,因为它基本上按设计工作)
当我创建这些视图时,我发现以下对我来说很奇怪的行为:
在我给视图充气后,getLayoutParameters()返回null.
在我调用addChild()将其添加到其父级之后,getLayoutParameters()返回一个有效的对象.
检查LayoutParameters,我发现宽度和高度都设置为-2(WRAP_CONTENT),这显然不是我在XML文件中指定的.
当我查看随附的ImageView的布局参数时,它会以指定的值读出.
谁能解释一下这里发生了什么?为什么我的指定高度没有被注意到?
这并没有真正影响我,因为父视图是一个自定义视图,其中我使用MeasureSpec等强制子项的最终尺寸,但我还是想了解这一点!
有两个公共接口:
LayoutInflater.Factory
和LayoutInflater.Factory2
在Android SDK中,但正式文件不能说这个接口,甚至一些有益的LayoutInflater
资料.
从我已经了解的情况来看,如果Factory2
已经设置,那么它将被使用,Factory
否则:
View view;
if (mFactory2 != null) {
view = mFactory2.onCreateView(parent, name, context, attrs);
} else if (mFactory != null) {
view = mFactory.onCreateView(name, context, attrs);
} else {
view = null;
}
Run Code Online (Sandbox Code Playgroud)
setFactory2()
也有非常简洁的文件:
/**
* Like {@link #setFactory}, but allows you to set a {@link Factory2}
* interface.
*/
public void setFactory2(Factory2 factory) {
Run Code Online (Sandbox Code Playgroud)
我应该使用哪家工厂如果我想将自定义工厂设置为LayoutInflater
?它们的区别是什么?
所以基本上我想在ViewPager中填充页面,为每个视图位置分别使用XML布局.我现在正在这样做
@Override
public Object instantiateItem(View container, int position) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
if(position == 0){
view = inflater.inflate(R.layout.main, null);
((ViewPager) container).addView(view, 0);
}
if(position == 1){
view = inflater.inflate(R.layout.main_second, null);
((ViewPager) container).addView(view, 0);
}
if(position == 2){
view = inflater.inflate(R.layout.main_third, null);
((ViewPager) container).addView(view, 0);
}
return view;
}
Run Code Online (Sandbox Code Playgroud)
首先正确显示视图,但是当我滑动ViewPager时,将隐藏/销毁布局.为什么是这样?我这样做是错误的吗?请帮助并纠正我.
谢谢.爱.韦斯利
我使用LayoutInflater扩展视图.我的膨胀RelativeLayout的背景颜色在我的xml文件中设置.我在我的一个设备上遇到了一个奇怪的问题:有时(随机)背景颜色是我的colors.xml中的另一种(错误的)颜色.以前有人遇到过这个问题吗?
我有ListView
一个CursorAdapter
.我使用以下代码只使用一个静态项目(所以我认为这不是回收问题)来扩充列表项目:
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
View v = vi.inflate(R.layout.bookmark_item, null) ;
//bindView(v, context, cursor) ;
Log.wtf("newView", "View color: " + Integer.toString(((ColorDrawable) (((RelativeLayout)v.findViewById(R.id.bookmark_row)).getBackground())).getColor())) ;
return v;
}
Run Code Online (Sandbox Code Playgroud)
我的layout/bookmark_item.xml
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="@+id/bookmark_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/app_menu_item_background"
tools:ignore="UselessParent" >
<TextView
android:id="@+id/bookmark_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
内部RelativeLayout的背景颜色设置为@color/app_menu_item_background
.这个颜色在我的R.java
:
public static final int app_menu_item_background=0x7f0a0036;
Run Code Online (Sandbox Code Playgroud)
我还有一个名为的颜色@color/app_menu_item_colored_background
,我在代码中的其他地方使用的颜色,它与我的书签列表和适配器没有任何共同之处.它还具有不同的资源ID …
在使用时膨胀元素时findViewById
,Android Studio始终警告我,我的膨胀视图可能会返回null
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
Run Code Online (Sandbox Code Playgroud)
并建议我做一些像环绕声我的语句与空检查:
if (v != null) {
mGridView = (GridView)v.findViewById(R.id.gridView);
}
Run Code Online (Sandbox Code Playgroud)
是否建议在膨胀元素之前始终执行上述的空检查?
编辑:添加lint图片
我正在开发一款Android应用.我有一个自定义视图和布局如下:
<com.hello.view.card.inner.SimpleCardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_simple_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/simple_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</com.hello.view.card.inner.SimpleCardView>
Run Code Online (Sandbox Code Playgroud)
这是Java类:
public class SimpleCardView extends LinearLayout {
protected SimpleCard card = null;
protected TextView textView;
public SimpleCardView(Context context) {
super(context);
init(context);
}
public SimpleCardView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public SimpleCardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
protected void init(Context context) {
textView = (TextView)findViewById(R.id.simple_label);
}
public void setCard(SimpleCard card) {
this.card = card;
textView.setText(card.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我如何夸大视图(我尝试了以下两个调用):
SimpleCardView …
Run Code Online (Sandbox Code Playgroud) 仍然是相对较新的我在我的活动类MainActivity中使用的非活动类MyLocation中找到视图时遇到问题.我正在使用MyLocation来获取经度和纬度.我想在使用GPS或网络时突出显示文本视图.为此,我需要在非活动类MyLocation中查找textviews.
以下是我在MainActivity中调用它的方式:
public class MainActivity extends ActionBarActivity implements LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
}
Run Code Online (Sandbox Code Playgroud)
在这里我在MyLocation中尝试查找textviews:
public class MyLocation {
LocationManager lm;
LocationResult locationResult;
private Context context;
TextView tvnetwork, tvgps;
private int defaultTextColor;
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
locationResult.gotLocation(location);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.main, null);
tvnetwork = (TextView) v.findViewById(R.id.tvnetwork);
tvgps = (TextView) v.findViewById(R.id.tvgps);
defaultTextColor = tvgps.getTextColors().getDefaultColor();
tvnetwork.setTextColor(context.getResources().getColor(
R.color.green)); …
Run Code Online (Sandbox Code Playgroud) 更新: 了解我的问题,这是我需要实现的:将图标从App抽屉拖动到主屏幕(如果可能的话,不是在gridview中),如图片中所示,
旧(这只是为了了解这是如何工作的):
我正在尝试将一个可点击的图标从一个拖动ListView
到一个customView
没有容器(Listview or Gridview...
)的同一个Activity或另一个,这里有一张图片供您了解:
但是当我把图标放在右边区域时我看不到对象,在日志中我看到:I/ViewRootImpl? Reporting drop result: true
我的代码:
class MyDragListener implements View.OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
...
case DragEvent.ACTION_DROP:
LinearLayoutAbsListView itemo = (LinearLayoutAbsListView)findViewById(R.id.paneko);
View child = getLayoutInflater().inflate(R.layout.list_item, null);
itemo.addView(child);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我的XML文件:
...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@android:color/background_dark"
android:orientation="horizontal" >
<com.moapps.elfassimounir.simple.LinearLayoutAbsListView
android:id="@+id/paneuj"
android:background="@android:color/background_light"
android:orientation="vertical"
> …
Run Code Online (Sandbox Code Playgroud) Update: I've changed the title to remove the indication that ExoPlayer has anything to do with what is going on as I've managed to duplicate this without it being used at all.
I decided to try and isolate this error on API levels:
I obtained an older Samsung tablet (Tab S2) running Android 7.0 (Api 24), and the error does not occur there.
I am not able to duplicate this problem on a Nexus 6 emulator using API 25
I …
nosuchmethoderror android-inflate layout-inflater android-studio android-typeface