想用两个LinearLayouts为我的应用程序拆分屏幕.我应该使用哪些参数来精确分割两个相等的部分 - 第一个是LinearLayout,第二个是在它下面.
H TextViews里面有多个LinearLayout.现在我想在java中删除这个文本视图.视图是动态生成的.我怎样才能清空这里面的所有内容LinearLayout?
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/myid">
<TextView .../>
<TextView .../>
<TextView .../>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 有没有办法获取LinearLayout的子元素?我的代码返回一个视图(linearlayout),但我需要访问布局中的特定元素.
有什么建议?
(是的,我知道我可以使用findViewById,但我在java中创建布局/子项 - 而不是XML.)
我有以下带有LinearLayout的main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1" android:id="@+id/llid">
<TextView android:text="Client profile"
android:id="@+id/ProfileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
<TextView android:text="Specs"
android:id="@+id/Specs"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我在运行时通过代码向LinearLayout添加一个图像
ImageView image = new ImageView(this);
image.setImageBitmap(bmp);
LinearLayout ll = (LinearLayout) findViewById(R.id.llid);
ll.addView(image);
Run Code Online (Sandbox Code Playgroud)
但是,我想在LinearLayout中的2个TextView之间添加ImageView.我似乎无法在Android文档中找到一种方法在另一个视图之前或之后添加视图.我怎样才能做到这一点?
NB我电话
setContentView(R.layout.main);
Run Code Online (Sandbox Code Playgroud)
在我将ImageView添加到LinearLayout之前.
我正在开发一个Android应用程序.我想更改LinearLayout元素的背景.
我可以设置什么属性来更改其背景?
我有以下代码生成片段,但只有当我将它们添加到我的XML文件中存在的线性布局时.
LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments);
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTransaction = fragMan.beginTransaction();
Fragment myFrag= new ImageFragment();
fragTransaction.add(R.id.foodItemActvity_linearLayout_fragments, myFrag , "fragment" + fragCount);
fragTransaction.commit();
Run Code Online (Sandbox Code Playgroud)
现在,如果我想将该片段添加到XML文件中尚不存在的线性布局,例如
LinearLayout rowLayout = new LinearLayout();
Run Code Online (Sandbox Code Playgroud)
第2部分:
Fragment frag1 = generateAppropriateFragment(type1);
Fragment frag2 = generateAppropriateFragment(type2);
LinearLayout fragmentsLayout = (LinearLayout) findViewById(R.id.foodItemActvity_linearLayout_fragments);
LinearLayout rowLayout = new LinearLayout(this);
rowLayout.setId(12345); // add counter to end
fragmentsLayout.addView(rowLayout);
getFragmentManager().beginTransaction().add(rowLayout.getId(), frag1, "fragment_grandchild" + fragCount).commit();
fragCount++;
getFragmentManager().beginTransaction().add(rowLayout.getId(), frag2, "fragment_grandchild" + fragCount).commit();
fragCount++;
Run Code Online (Sandbox Code Playgroud) 我正在通过扩展来创建自定义小部件LinearLayout:
public class MyWidget extends LinearLayout {
private static Paint PAINT = new Paint(Paint.ANTI_ALIAS_FLAG);
static {
PAINT.setColor(Color.RED);
}
public MyWidget(Context context) {
this(context, null);
}
public MyWidget(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight()/2, canvas.getWidth()/2, PAINT);
// never gets called :-(
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// this gets called, but with a canvas sized after the padding.
}
}
Run Code Online (Sandbox Code Playgroud)
我可以添加孩子就好了,但我从来没有让我的自定义onDraw()被调用.dispatchDraw() …
大家好我刚刚在LinearLayout中实现了ListView,但我需要定义LinearLayout的高度(它必须是屏幕高度的50%).
<LinearLayout
android:id="@+id/widget34"
android:layout_width="300px"
android:layout_height="235px"
android:orientation="vertical"
android:layout_below="@+id/tv_scanning_for"
android:layout_centerHorizontal="true">
<ListView
android:id="@+id/lv_events"
android:textSize="18sp"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_scanning_for"
android:layout_centerHorizontal="true">
</ListView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这有可能吗?
我为一个按钮和一个EditText做了类似的事情,但似乎不适用于Layouts.
这是我的代码:
//capture the size of the devices screen
Display display = getWindowManager().getDefaultDisplay();
double width = display.getWidth();
//my EditText will be smaller than full screen (80%)
double doubleSize = (width/5)*4;
int editTextSize = (int) doubleSize;
//define the EditText
userName = (EditText) this.findViewById(R.id.userName);
password = (EditText) this.findViewById(R.id.password);
//set the size
userName.setWidth(editTextSize);
password.setWidth(editTextSize);
Run Code Online (Sandbox Code Playgroud)
谢谢!:)
size android android-layout android-linearlayout android-layout-weight
我正在尝试向ExpandableListView添加标头,如下所示:
headerView = View.inflate(this, R.layout.header, null);
expandableListView.addHeaderView(headerView);
expandableListView.setAdapter(new SectionedAdapter(this));
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
12-08 16:23:42.354:
ERROR/AndroidRuntime(421): Caused by:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.clearRecycledState(ListView.java:504)
12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.resetList(ListView.java:490)
12-08 16:23:42.354:ERROR/AndroidRuntime(421):at android.widget.ListView.setAdapter(ListView.java:422)
12-08 16:23:42.354:ERROR/AndroidRuntime(421): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)
Run Code Online (Sandbox Code Playgroud)
这是在电话会议上发生的expandableListView.setAdapter(new SectionedAdapter(this)),但我无法弄清楚原因.有任何想法吗?
android classcastexception expandablelistview android-linearlayout
对不起,巨大的代码转储,但我真的迷路了.
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