我有一个课程View1
延伸View
.我想R.layout.test2.xml
在这堂课中充气View1
.我在这个类中添加了以下代码
public class View1 extends View {
View view;
String[] countries = new String[] {"India", "USA", "Canada"};
public View1( Context context) {
super(context);
LayoutInflater mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=mInflater.inflate(R.layout.test2, null, false);
}
}
Run Code Online (Sandbox Code Playgroud)
从另一个类Home
我希望这个膨胀的视图在某些情况下存在,在Home
类中我编写了以下代码:
public class Home extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
CreateView();
}
public void CreateView() {
LinearLayout lv=(LinearLayout)findViewById(R.id.linearlayout);
View1 view = new View1(Home.this);
lv.addView(view);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行我的项目时,活动并没有向我显示任何内容.
这是我的代码:
input.xml(布局文件夹)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/background_main" >
<ImageView
android:id="@+id/logo_image"
android:background="@drawable/background_green"
android:src="@drawable/titleimage"
android:layout_width="match_parent"
android:layout_height="50dp"
/>
<ScrollView
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/logo_image"
android:layout_marginTop="10dp"
>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="@+id/device_type_row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginRight="5dp" >
<RadioButton
android:id="@+id/device_type_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/device_type" />
<RadioGroup
android:id="@+id/device_type_radio_selection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radioIos"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/iOS"
/>
<RadioButton
android:id="@+id/radioAndroid"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/android"
/>
</RadioGroup>
</TableRow>
<TableRow
android:id="@+id/days_as_customers_row"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_marginRight="5dp" >
<RadioButton
android:id="@+id/days_as_customers_radio" …
Run Code Online (Sandbox Code Playgroud) 我试图给包含Fragment
使用向后兼容包的布局充气.我把jar文件放在我项目的libs文件夹中.我扩展了Fragment,然后尝试通过将Activity的contentView设置为来扩充它
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<fragment
class="com.test.fragments.AdFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/></LinearLayout>
Run Code Online (Sandbox Code Playgroud)
但是当我设置内容视图时,它会ClassNotFoundException
因为片段标记而失败.这是logcat输出.
java.lang.RuntimeException: Unable to start activity ComponentInfo{}: \
android.view.InflateException: Binary XML file line #51: \
Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1777)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1793)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3848)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #51: \
Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320) …
Run Code Online (Sandbox Code Playgroud) android backwards-compatibility android-inflate android-fragments
我有一个LinearLayout,有很多嵌套的LinearLayouts和TextViewss
我的主要活动是膨胀主要的LinearLayout,
然后我从服务器加载数据,并根据收到的数据,我在占位符(LinearLayout)中添加多个布局
这是一个简单的新闻页面,我加载与新闻相关联的图像并将其放在最初为空的LinearLayout中.
每个图像都有以下信息:标题(TextView),日期(TextView),图像(ImageView)所以我实际做的是以下内容:
*请注意,这只是在我提出的所有尝试的问题中必不可少的代码 - > catch ... if/else ....等
public void addImages(JSONArray images){
ViewGroup vg = (ViewGroup) findViewById(R.id.imagesPlaceHolder);
// loop on images
for(int i =0;i<images.length;i++){
View v = getLayoutInflater().inflate(R.layout.image_preview,vg);
// then
I think that here is the problem
ImageView imv = (ImageView) v.findViewById(R.id.imagePreview);
TextView dt = (TextView) v.findViewById(R.id.dateHolder);
TextView ttl = (TextView) v.findViewById(R.id.title);
// then
dt.setText("blablabla");
ttl.setText("another blablabla");
// I think the problem is here too, since it's referring to a single image
imv.setTag( images.getJSONObject(i).getString("image_path").toString() ); …
Run Code Online (Sandbox Code Playgroud) 我试图通过返回自己的视图层次结构来自定义片段布局onCreateView(LayoutInflater, ViewGroup, Bundle)
.这膨胀了我的自定义视图,但似乎堆叠视图而不是在内部膨胀,我立刻看到了所有内容.任何帮助表示赞赏.
MyActivity.java:
public class MyActivity extends FragmentActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
ArrayListFragment list = new ArrayListFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit();
}
}
public static class ArrayListFragment extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
inflater.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.main, container);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String List[] = {"Larry", "Moe", "Curly"};
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, List));
}
}
}
Run Code Online (Sandbox Code Playgroud)
main.xml中
<?xml version="1.0" …
Run Code Online (Sandbox Code Playgroud) android android-layout android-listview android-inflate android-fragments
我有这个代码
View item = View.inflate(context, R.layout.item_layout, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
layout.addView(item, params);
Run Code Online (Sandbox Code Playgroud)
我的item_layout :(注意部分android:layout_marginTop ="2dip")
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_marginTop="2dip" android:layout_width="fill_parent">
<ImageView android:src="@drawable/pic_unknown" android:id="@+id/image1"
android:layout_height="50dip" android:layout_width="50dip"
android:padding="5dip"></ImageView>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
然后在我的布局中,我看到了项目列表,但它们之间没有任何边距.我尝试使用margintop = 10dip仍然没有发生我的观点是我在布局中放置的值不是在计算中有或没有边距顶部布局是相同的.
如何在项目之间添加一些空白区域?如何在物品之间充气?是否有可能膨胀像空隙或某些空间?或者我必须使用解决方法,比如用2dip高度或某些东西来膨胀一些空布局谢谢
我有一个从main_menu.xml膨胀的菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/act_sync"
android:showAsAction="always"
android:actionLayout="@layout/sync_action"
android:icon="@android:drawable/ic_popup_sync" />
</menu>
Run Code Online (Sandbox Code Playgroud)
这是活动中的代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
MyMessageHandler.debug("menu item selected");
switch(item.getItemId()){
case R.id.act_sync:
sync();
return true;
}
return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
但是当我触摸菜单项时,不会调用onOptionsItemSelected.当我删除菜单项的actionLayout属性时,它工作正常.我怎样才能解决这个问题?
谢谢.
android menuitem android-layout android-inflate android-actionbar
我正在创建一个tableLayout [以XML格式给出]
添加表行[以XML格式创建并在Java中膨胀]
还在表格中添加2个textview [以XML格式创建并在JAVA中进行膨胀]
我只能获得背景和文本颜色,但不能获得宽度,高度和边距等布局属性来获取表格视图.
user-interface android view android-inflate android-tablelayout
我正在开发一个应用程序,我必须onClick()
在单击操作栏自定义视图时获取事件.到目前为止,我能够实现以下布局.
这是我实现此目的的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setCustomView(R.layout.custom_image_button);
getActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(), "Clicked on ActionBar",
Toast.LENGTH_SHORT).show();
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的custom_image_button布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<TextView
android:id="@+id/points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/points_yellow"
android:gravity="center"
android:paddingLeft="20dp"
android:textColor="#887141"
android:textIsSelectable="false"
android:textSize="22sp"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="@+id/badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginRight="5dp" …
Run Code Online (Sandbox Code Playgroud) android android-layout android-inflate android-actionbar onclicklistener
我想膨胀包含单个搜索栏的布局。通过这种方式,我创建了一个扩展片段类的java类,然后我膨胀了布局。但出现以下错误:
\n二进制 XML 文件第 #9 行:二进制 XML 文件第 #9 行:膨胀类 com.google.android.material.slider.Slider 时出错
\nwarming.XML is in the following:\n\n <?xml version="1.0" encoding="utf-8"?>\n<RelativeLayout\n xmlns:android="http://schemas.android.com/apk/res/android"\n xmlns:app="http://schemas.android.com/apk/res-auto"\n android:layout_width="match_parent"\n android:layout_height="match_parent">\n\n <com.google.android.material.slider.Slider\n android:layout_width="327dp"\n android:layout_height="37dp"\n android:layout_alignParentStart="true"\n android:layout_alignParentTop="true"\n android:layout_alignParentEnd="true"\n android:layout_alignParentBottom="true"\n android:layout_marginStart="42dp"\n android:layout_marginTop="217dp"\n android:layout_marginEnd="42dp"\n android:layout_marginBottom="349dp"\n android:valueFrom="0"\n android:valueTo="300"\n app:activeTrackColor="#ff4500"\n app:inactiveTrackColor="#fbb999" >\n\n </com.google.android.material.slider.Slider>\n</RelativeLayout>\n
Run Code Online (Sandbox Code Playgroud)\n和firstPage.java 膨胀布局warming.XML 如下:
\npublic class firstPage extends Fragment {\n @Nullable\n @Override\n public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {\n return inflater.inflate(R.layout.warming,container,false);\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n使用以下代码,我想执行firstPage.java类:
\ngetSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new firstPage()).commit();\n
Run Code Online (Sandbox Code Playgroud)\n怎么了?谁能帮我?我在StackOverflow和其他一些网站上搜索过这个问题,也有类似的问题,但没有人能解决我的问题。
\n更新 …
android ×10
android-inflate ×10
java ×2
view ×2
xml ×2
android-view ×1
fragment ×1
inflate ×1
margin ×1
menuitem ×1