English 中文(简体)
动态调整大小 碎片和机器人
原标题:dynamic resize fragment android

我想更改我在《Mai Acticity》中使用的碎片的宽度? 但我找不到它的 < code> LayoutParams 来更改默认宽度 :

public class MainActivity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int widthFfragment = 300;

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        widthTot = size.x;
        heightTot = size.y;

        findViewById(R.id.f1).getLayoutParams().width = widthTot-widthFfragment;
    }
}

这是我的碎片代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/f1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#330000"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

<强力 > 碎片化1.java

public class Fragment1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        //---Inflate the layout for this fragment---
        return inflater.inflate( R.layout.fragment1, container, false);
    }
}

非常感谢。

问题回答

您可以与从fragment.getView () 获取的视图的布局Params 实例合作来做到这一点。 我的样本代码 :

private void resizeFragment(Fragment f, int newWidth, int newHeight) {
    if (f != null) {
        View view = f.getView();
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(newWidth, newHeight);
        view.setLayoutParams(p);
        view.requestLayout();       


    }
}

可用如下:

resizeFragment(myFragment, 200, 500);
resizeFragment(otherFragment, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

您的右侧, 碎片没有布局Params, 因为它是一个概念性容器, 您需要调整碎片中膨胀视图的布局参数, 或者调整您连接碎片的容器 。

public class Fragment1 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ViewGroup view = (ViewGroup) inflater.inflate( R.layout.fragment1, container, false);

    // TODO Adjust layout params of inflated view here

    return view;
}

示例假设您正在充气查看组 。

希望这有帮助。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签